20161028
Display a ListView in a Fragment //BlankFragment.java package com.example.kunihiro.myapplication; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ListView; /** * A simple {@link Fragment} subclass. */ public class BlankFragment extends Fragment { public BlankFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view= inflater.inflate(R.layout.fragment_blank, container, false); String[] menuItems={"a","b","c","d","e"}; ListView listView=(ListView)view.findViewById(R.id.ListViewInFragmentBlankXml); ArrayAdapter<String> listViewAdapter=new ArrayAdapter<String>( getActivity(), android.R.layout.simple_list_item_1, menuItems); listView.setAdapter(listViewAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position == 0){
Toast.makeText(getActivity(),"You clicked the first item",Toast.LENGTH_LONG).show();
}else if(position == 1){
Toast.makeText(getActivity(),"You clicked the second item",Toast.LENGTH_LONG).show();
}


}
});
return view;
}

}
 //MainActivity package com.example.kunihiro.myapplication; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); BlankFragment blankFragment=new BlankFragment(); getSupportFragmentManager().beginTransaction().add(R.id.activity_main,blankFragment).commit(); } } //fragment_blank <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.kunihiro.myapplication.BlankFragment"> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/ListViewInFragmentBlankXml"/> </FrameLayout>


ListView 20160805

---MainActivity --
import
android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends ListActivity {
private String[] listStrings = { "list1", "list2", "list3", "list4" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, listStrings);
setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView listView, View v, int position,long id) {
super.onListItemClick(listView, v, position, id);
String item = (String) listView.getItemAtPosition(position);
Toast.
makeText(getApplicationContext(),item, Toast.LENGTH_SHORT).show();
}
}