Hello.

I need to "thrust" a custom ListView into one of the fragments.

Fragment1.java

public class Fragment1 extends Fragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.four, null); final ListView list = (ListView) v.findViewById(R.id.list); list.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { } public void onCreate (Bundle icicle) { // Адаптер ArrayList<HashMap<String, String>> myArrList = new ArrayList<HashMap<String, String>>(); HashMap<String, String> map; map = new HashMap<String, String>(); map.put("Name", "1. Русский язык"); map.put("Tel", "4"); SimpleAdapter adapter = new SimpleAdapter( getActivity(), myArrList, R.layout.row, new String[]{"Name", "Tel"}, new int[]{R.id.text1, R.id.text2}); list.setAdapter(adapter); } }); return v; } } 

On the emulator, the application starts, but then crashes.

What needs to be fixed?

  • one
    Look in the log, in which line the error falls out? - Werder
  • Add a glass structure. You think telepaths are full here, guess what kind of mistake you have there - pavlofff
  • 3
    Where did the OnClickListener method onCreate ? - temq 3:31 pm

1 answer 1

I got to myself. I answer my own question, all of a sudden someone will need it:

My Fragment2.java:

 public class Fragment2 extends Fragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.five, null); ListView listView = (ListView) v.findViewById(R.id.listView); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { } }); ArrayList<HashMap<String, String>> myArrList = new ArrayList<HashMap<String, String>>(); HashMap<String, String> map; map = new HashMap<String, String>(); map.put("Name", "Алгебра"); map.put("Tel", "4.45"); myArrList.add(map); map=new HashMap<String, String>(); map.put("Name","Английский язык"); map.put("Tel","4.55"); myArrList.add(map); SimpleAdapter adapter = new SimpleAdapter( getActivity(), myArrList, R.layout.row, new String[]{"Name", "Tel"}, new int[]{R.id.text1, R.id.text2}); listView.setAdapter(adapter); return v; } } 

Markup on row.xml:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <TextView android:id="@+id/text1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Name" android:textSize="20dp" android:textColor="@color/blue" android:layout_marginTop="20dp" android:layout_marginBottom="20dp" android:layout_marginLeft="10dp"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/RelLay" android:layout_marginRight="25dp"> <TextView android:id="@+id/text2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tel" android:textSize="20dp" android:layout_alignParentRight="true" android:textColor="@color/blue" android:layout_marginTop="20dp" android:layout_marginBottom="20dp" android:textStyle="bold" /> </LinearLayout> </LinearLayout>