gpt4 book ai didi

android - 选择同一组的多个相关行 onlongclick ListView

转载 作者:行者123 更新时间:2023-11-29 16:47:40 26 4
gpt4 key购买 nike

我计划有一个 ListView ,当单击时,将能够选择所有子行/相关行。

例子

输入:长按用户名/年龄/生日输出:返回用户名、年龄和生日。

问题

长按只会返回用户名,不会返回年龄和生日。

我没有在 toast 中显示年龄和生日值,因为应用会显示错误并停止运行。

最新:已在 17 年 11 月 26 日展示了 Adapter 和对象类

ListView 的自定义布局

<?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">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username:"
android:background="#FF2400"
android:textColor="#FFFFFF" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text_user_name"
android:textColor="#FFFFFF"
android:background="#FF2400"
android:text="Username"
android:gravity="left"/>

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Age:"
android:background="#FFFFFF"
android:textColor="#000000" />

<EditText
android:layout_width="match_parent"
android:layout_weight="0.5"
android:textColor="#000000"
android:layout_height="wrap_content"
android:id="@+id/text_user_age"
android:background="#FFFFFF"
android:text="Age"
android:gravity="left"/>

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Birthday:"
android:background="#FFFFFF"
android:textColor="#000000" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:textColor="#000000"
android:background="#FFFFFF"
android:id="@+id/text_user_birthday"
android:text="Birthday"
android:gravity="left"/>
</LinearLayout>
</LinearLayout>

DataListActivity( ListView Activity )

    listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener(){
public boolean onItemLongClick(AdapterView<?> arg0, View v,
int index, long arg3)
{

AlertDialog.Builder builder = new AlertDialog.Builder(DataListActivity.this);
builder.setTitle("Notice");
builder.setMessage("Launching this missile will destroy the entire universe. Is this what you intended to do?");
final View clicklist = v;
final int position = index;

builder.setPositiveButton("Launch missile", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String selname= ((EditText) clicklist.findViewById(R.id.text_user_name)).getText().toString();
// String selage= ((EditText) clicklist.findViewById(R.id.text_user_age)).getText().toString();
String selbirthday= ((EditText) clicklist.findViewById(R.id.text_user_birthday)).getText().toString();
Toast.makeText(DataListActivity.this,selname + selage + selbirthday,Toast.LENGTH_LONG).show();
}
});
builder.setNegativeButton("Cancel", null);

AlertDialog dialog = builder.create();
dialog.show();
return false;
}
});

布局适配器

public class ListDataAdapter extends ArrayAdapter{
List list = new ArrayList();

static class LayoutHandler
{
TextView NAME,AGE,BIRTHDAY;
}
public ListDataAdapter(Context context, int resource) {
super(context, resource);
}

@Override
public void add (Object object)
{
super.add(object);
list.add(object);
}

@Override
public int getCount(){
return list.size();
}

@Override
public Object getItem(int position)
{
return list.get(position);
}

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

View row = convertView;
LayoutHandler layoutHandler;
if (row==null)
{
LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.row_layout,parent,false);
layoutHandler = new LayoutHandler();
layoutHandler.NAME = (TextView) row.findViewById(R.id.text_user_name);
layoutHandler.AGE = (TextView) row.findViewById(R.id.text_user_age);
layoutHandler.BIRTHDAY = (TextView) row.findViewById(R.id.text_user_birthday);
}
else
{
layoutHandler = (LayoutHandler) row.getTag();
}
DataProvider dataProvider = (DataProvider) this.getItem(position);
layoutHandler.NAME.setText(dataProvider.getName());
layoutHandler.AGE.setText(dataProvider.getAge());
layoutHandler.BIRTHDAY.setBIRTHDAY(dataProvider.getBirthday());


return row;
}

@Override
public void clear()
{
list.clear();
}

public class DataProvider {

private String Name;
private String Age;
private String Birthday;

....getter 和 setter 方法

最佳答案

分析你的代码后,我发现了以下问题:-

在 List 适配器中,您没有将标签设置为 row 所以它总是返回 null layoutHandler 并且您将 EditText 转换为 TextView ,我在这里没有明白您的意思如果你想要 TextView 然后在 XML 中使用相同的 View 。

所以 ListAdapter 看起来像这样。

public class ListAdapter extends ArrayAdapter {
List list = new ArrayList();

static class LayoutHandler
{
EditText NAME,AGE,BIRTHDAY;
}
public ListAdapter(Context context, int resource) {
super(context, resource);
}

@Override
public void add (Object object)
{
super.add(object);
list.add(object);
}

@Override
public int getCount(){
return list.size();
}

@Override
public Object getItem(int position)
{
return list.get(position);
}

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

View row = convertView;
LayoutHandler layoutHandler;
if (row==null)
{
LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.custom_layout,parent,false);
layoutHandler = new LayoutHandler();
layoutHandler.NAME = (EditText) row.findViewById(R.id.text_user_name);
layoutHandler.AGE = (EditText) row.findViewById(R.id.text_user_age);
layoutHandler.BIRTHDAY = (EditText) row.findViewById(R.id.text_user_birthday);
row.setTag(layoutHandler);
}
else
{
layoutHandler = (LayoutHandler) row.getTag();
}
DataProvider dataProvider = (DataProvider) this.getItem(position);
layoutHandler.NAME.setText(dataProvider.getName());
layoutHandler.AGE.setText(dataProvider.getAge());
layoutHandler.BIRTHDAY .setText(dataProvider.getBirthday());


return row;
}

@Override
public void clear()
{
list.clear();
}


}

然后进入实际问题,即 ListView OnItemLongClickListener 崩溃。你只需要改变

final View clicklist = v;

final View clicklist = arg0.getChildAt(index);

然后你就走了。 :)

关于android - 选择同一组的多个相关行 onlongclick ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47379875/

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