gpt4 book ai didi

java - View.gettag 返回 null,我在 ListView/ArrayAdapter 设置中错过了什么?

转载 作者:行者123 更新时间:2023-12-01 17:14:52 29 4
gpt4 key购买 nike

我有一个自定义类“Something”,其中包含一些整数和字符串。我正在制作一个页面,允许用户查看每个当前 Something 的描述,以及每个描述旁边的用于编辑或删除的按钮。

我已经重写 Something 类中的 toString(),以便它可以返回要放入 TextView 字段的描述。

list_task_edit.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".EditTaskList">
...
<ListView
android:id="@+id/edit_task_listview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>

...
</LinearLayout>

edit_task_list_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<TextView
android:id="@+id/edit_task_name"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
/>

<Button
android:id="@+id/edit_task_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_edittask"
android:onClick=""
/>

<Button
android:id="@+id/edit_task_delete"
android:layout_width="wrap_content"
android:text="@string/button_delete_task"
android:layout_height="wrap_content"
android:onClick="deleteTask"
/>

</LinearLayout>

(我还没有为 editTask 添加方法,因为我还没有做到这一点)

public class EditTaskList extends Activity {

private static ArrayList<Something> list;

ArrayAdapter<Something> arrayAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_task_edit);
list = MainActivity.getMasterList();

arrayAdapter = new ArrayAdapter<Something>
(this,
R.layout.edit_task_list_item,
R.id.edit_task_name,
list);
ListView listView = (ListView)findViewById(R.id.edit_task_listview);
listView.setAdapter(arrayAdapter);
}

public void deleteTask(View view){
Something thisItem = (Something)view.getTag();
if(thisItem == null){
System.out.println("Bad things");
}
arrayAdapter.remove(thisItem);
arrayAdapter.notifyDataSetChanged();
}
...
}

我的诊断行用于检查 thisItem == null 是否每次都会关闭。我缺少什么?我是否以某种方式声明了 ArrayAdapter 错误?

最佳答案

更新后的工作代码。如果对其他人有用:

public class EditTaskList extends Activity {

private static ArrayList<Something> list;
private CustomAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_task_edit);
ListView listView = (ListView)findViewById(R.id.edit_task_listview);
list = MainActivity.getMasterList();
adapter = new CustomAdapter(this, list);
listView.setAdapter(adapter);
}

public void resetAdapter(){
adapter.notifyDataSetChanged();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.edit_task_list, menu);
return true;
}

public class CustomAdapter extends ArrayAdapter<Something>{
private ArrayList<Something> things;
private LayoutInflater inflater;

class ViewHolder {
public TextView text;
public Button editButton;
public Button deleteButton;
}

public CustomAdapter(Context context, ArrayList<Something> things) {
super(context, R.layout.edit_task_list_item, things);
inflater = LayoutInflater.from(context);
this.things = things;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

// reuse views
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = inflater.inflate(R.layout.edit_task_list_item, null);
// configure view holder
viewHolder.text = (TextView) convertView.findViewById(R.id.edit_task_name);
viewHolder.editButton = (Button) convertView.findViewById(R.id.edit_task_edit);
viewHolder.deleteButton = (Button) convertView.findViewById(R.id.edit_task_delete);
convertView.setTag(viewHolder);
}

else {
viewHolder = (ViewHolder) convertView.getTag();
}

viewHolder.text.setTag(position);
viewHolder.text.setText(things.get(position).toString());
viewHolder.deleteButton.setTag(position);
viewHolder.editButton.setTag(position); //currently does nothing

viewHolder.deleteButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View view) {
int tag = (Integer)view.getTag();
things.remove(tag);
resetAdapter();
}
});

return convertView;
}
}
}

关于java - View.gettag 返回 null,我在 ListView/ArrayAdapter 设置中错过了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22676743/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com