我想创建一个ListView
,其中每个row
都有两个按钮。 Button1
和Button2
。
我已经阅读了很多关于如何创建CustomAdapter
或扩展BaseAdapter
的教程,但是在我的案例中没有一个有用,这就是我在这里开一个新问题的原因。
我的strings.xml
:
<string-array name="locations_array">
<item>Item1</item>
<item>Item2</item>
</string-array>
我想在我的ListView中使用这个数组(我设法使用onClick事件创建一个普通的ListView,但现在我想为每一行添加按钮)并为每一行添加两个按钮,每个按钮都有自己的onClick事件。
我已经按照this answers here上的步骤和网上的其他一些教程进行了操作,但在尝试NullPointerException
时,我总是得到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;
}
}
在这里我使用片段中的适配器:
locationsArray = getResources().getStringArray(R.array.locations_array);
ArrayList<String> data = new ArrayList<>(Arrays.asList(locationsArray));
CustomAdapter adapter = new CustomAdapter(data,getActivity());
listView.setAdapter(adapter);
但这不起作用。所有教程都专注于我需要的其他东西。我只需要在每行上放两个按钮,使用现有的String Array。我不想动态添加任何东西等。只需使用我已创建的字符串数组。
编辑(添加我忘记的XML文件!)
R.layout.layout_listview(EditText用于过滤列表):
<?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异常:
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)
代码行:
listItemText.setText(list.get(position));
希望有人可以帮助我。
干杯
答案 0 :(得分:1)
请使用ViewHolder模式。你需要这些:
item_listrow.xml:
<LinearLayout
android:layout_height = "match_parent";
andorid:layout_width = "match_parent";
andorid:orientation = "horizontal";
>
<Button
android:id = "@+id/button1"
android:layout_height = "match_parent";
andorid:layout_width = "0dp";
andorid:layout_weight = 1;
/>
<Button
android:id = "@+id/button2"
android:layout_height = "match_parent";
andorid:layout_width = "0dp";
andorid:layout_weight = 1;
/>
</LinaerLayout>
你的Adapter getView()方法应如下所示:
@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_listrow,null);
Holder h = new Holder();
h.button1 = (Button) view.findViewById(R.id.button1);
h.button2 = (Button) view.findViewById(R.id.button2);
view.setTag(h);
}
Holder holder = (Holder) view.getTag();
holder.button1.setText(list.get(0));
holder.button2.setText(list.get(1));
listItemText.setText(list.get(position));
holder.button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
holder.button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
return view;
}
上面使用的Holder类:(只是将它嵌套在Adapter类中)
public static class Holder {
Button button1;
Button button2;
}
以下是基于给定方法隐藏按钮的代码isDeveloperMode()
:
@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_listrow,null);
Holder h = new Holder();
h.button1 = (Button) view.findViewById(R.id.button1);
h.button2 = (Button) view.findViewById(R.id.button2);
view.setTag(h);
}
Holder holder = (Holder) view.getTag();
if(isDeveloperMode()){
holder.button1.setVisibility(View.VISIBLE);
holder.button2.setVisibility(View.VISIBLE);
holder.button1.setText(list.get(0));
holder.button2.setText(list.get(1));
listItemText.setText(list.get(position));
holder.button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
holder.button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
} else {
holder.button1.setVisibility(View.GONE);
holder.button2.setVisibility(View.GONE);
}
return view;
}
答案 1 :(得分:1)
请尝试以下方法:
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/lvItems"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
MainActivity.java:
public class MainActivity extends Activity {
private ListView lvItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lvItems = (ListView) findViewById(R.id.lvItems);
String[] locationsArray = getResources().getStringArray(
R.array.locations_array);
CustomAdapter adapter = new CustomAdapter(this, locationsArray);
lvItems.setAdapter(adapter);
lvItems.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
ViewHolder holder = (ViewHolder) view.getTag();
String item = holder.getItem();
// Do what ever with your Item.
// If You need the position, you can take it from above
// position.
}
});
}
}
list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >
<Button
android:id="@+id/btnLocalData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="local Data" />
<TextView
android:id="@+id/tvItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/btnLocalData"
android:layout_alignTop="@+id/btnOnlineData"
android:layout_toLeftOf="@+id/btnOnlineData"
android:layout_toRightOf="@+id/btnLocalData"
android:gravity="center"
android:text="Item"
android:textSize="16dp"
android:textStyle="bold" />
<Button
android:id="@+id/btnOnlineData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Online Data" />
</RelativeLayout>
CustomAdapter.java:
public class CustomAdapter extends ArrayAdapter<String> {
private LayoutInflater lf;
public CustomAdapter(Context context, String[] objects) {
super(context, 0, objects);
lf = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = lf.inflate(R.layout.list_item, parent, false);
holder = new ViewHolder();
holder.btnLocalData = (Button) convertView
.findViewById(R.id.btnLocalData);
holder.btnOnlineData = (Button) convertView
.findViewById(R.id.btnOnlineData);
holder.tvItem = (TextView) convertView.findViewById(R.id.tvItem);
holder.initListeners();
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.setData(getItem(position));
return convertView;
}
public static class ViewHolder {
TextView tvItem;
Button btnOnlineData;
Button btnLocalData;
String mItem;
public String getItem(){
return mItem;
}
public void setData(String item) {
mItem = item;
tvItem.setText(item);
}
public void initListeners() {
btnLocalData.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(),
"Local Data Clicked : " + mItem, Toast.LENGTH_LONG)
.show();
}
});
btnOnlineData.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(),
"Online Data Clicked : " + mItem, Toast.LENGTH_LONG)
.show();
}
});
}
}
}
答案 2 :(得分:0)
在适配器
中将代码更改为以下内容@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView== null){
LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView= inflater.inflate(R.layout.layout_listview,null);
}
Log.d("TAG", list.get(position));
TextView listItemText = (TextView) convertView.findViewById(R.id.list_item_string);
listItemText.setText(list.get(position));
Button localData = (Button) convertView.findViewById(R.id.button1);
Button onlineData = (Button) convertView.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 convertView;
}
答案 3 :(得分:0)
尝试代替
@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);
使用
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
Log.d("TAG", list.get(position));
TextView listItemText = (TextView) view.findViewById(R.id.list_item_string);