我计划有一个列表视图,当onlongclick时,将能够选择所有子行/相关行。
输入: 长按用户名/年龄/生日 输出: 返回用户名,年龄和生日。
如果您长时间点击用户名而不是年龄和生日,则只返回用户名。
我没有在toast中显示年龄和生日值,导致应用显示错误并停止运行。
最新: 已于11月26日展示了适配器和对象类
列表视图的自定义布局
<?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活动)
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;
}
});
LayoutAdapter
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和setters方法
答案 0 :(得分:1)
更好的方法是
1)为示例数据创建自定义类并添加您的属性 想要在那个班级中展示。
2)创建一个Adapter并使用Data类的arraylist填充你的适配器。
3)使用onItemClicklistener从listview中检索值。
答案 1 :(得分:0)
修改你的吐司
?x,y