I want to create a ListView
where each row
would have two buttons. Button1
and Button2
.
I have read few tutorials on how to create CustomAdapter
or extend BaseAdapter
, but none of them worked in my case, that is why I'm opening a new question here.
I have a static (not changing) String Array in my strings.xml
:
<string-array name="locations_array">
<item>Item1</item>
<item>Item2</item>
</string-array>
I want to use this Array in my ListView ( I manage to create a normal ListView with onClick event, but now I want to add buttons to each row ) and add two buttons to each row where each will have their own onClick event.
I have followed steps from this answers here and few other tutorials on web, but I always get NullPointerException
when trying to setText
.
CustomAdapter.java:
public class CustomAdapter extends BaseAdapter implements ListAdapter {
private ArrayList<String> list = new ArrayList<>();
private Context ctx;
public CustomAdapter(ArrayList<String> list, Context ctx){
this.list = list;
this.ctx = ctx;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
Log.d("TAG", list.get(position));
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if(view == null){
LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.layout_listview,null);
}
Log.d("TAG", list.get(position));
TextView listItemText = (TextView) view.findViewById(R.id.list_item_string);
listItemText.setText(list.get(position));
Button localData = (Button) view.findViewById(R.id.button1);
Button onlineData = (Button) view.findViewById(R.id.button2);
localData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
onlineData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
return view;
}
}
And here I use the adapter in my Fragment:
locationsArray = getResources().getStringArray(R.array.locations_array);
ArrayList<String> data = new ArrayList<>(Arrays.asList(locationsArray));
CustomAdapter adapter = new CustomAdapter(data,getActivity());
listView.setAdapter(adapter);
But this doesn't work. All tutorials are focusing on something else then what I need. I just need simply to put two buttons on each row, with existing String Array. I dont want to add anything dynamically etc. Just use that String Array that I have already created.
EDIT (adding XML files I forgot!)
R.layout.layout_listview (the EditText is to filter the List):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/et_filter"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:hint="Search Location">
</EditText>
<ListView
android:id="@+id/list"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</ListView>
</LinearLayout>
R.layout.layout_listview_buttons:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/list_item_string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:paddingLeft="8dp"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="@+id/button1"
android:layout_width="80dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="Online Data"
android:textColor="#0099CC" />
<Button
android:id="@+id/button2"
android:layout_width="80dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_below="@+id/button1"
android:layout_marginTop="3dp"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="Local Data"
android:textColor="#0099CC" />
</RelativeLayout>
LogCat Exception:
12-15 11:24:10.852 14626-14626/com.inodroid.myweatherapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.inodroid.myweatherapp, PID: 14626
java.lang.NullPointerException
at com.inodroid.myweatherapp.Data.CustomAdapter.getView(CustomAdapter.java:59)
Line in code:
listItemText.setText(list.get(position));
Hope someone can help me out here.
Cheers
view = inflater.inflate(R.layout.layout_listview,null);
toview = inflater.inflate(R.layout.layout_listview_buttons,null);
– Dugger