根据 Android 中选择的项目更新 ListView 中的子项目
Update sub item in ListView according to item selected in Android
我想用 select 在不同 activity 中编辑的项目更新子项目文本。这个怎么解决?
实际上这就是我想要做的:
我有一个名为 MainListview 的列表视图,具有不同的标题(日常使用、蔬菜、汽车用品)。我正在尝试根据子项 activity
中的项 selected 设置子项文本
与图片相似的内容:
如果我点击 Veggies(另一个 activity with Listview 并在那里打开了复选框,我 select tomato and Potatoe 然后点击 OK。我现在应该在子项目中得到 tomato, Potatoe子项显示相同的值 Totmato、Potatoe
如何实现以上逻辑?
有人可以帮我解决这个问题吗?
现在:我正在做这样的事情:
listofitems = new itemDetails(iTemName, subitemtext.toString());
listofitemByList.add(listofitems);
adapter = new ItemListAdapter(getApplicationContext(), R.layout.list_row, listofitemByList);
list.setAdapter(adapter);
我在 MainListView 中得到以下错误结果:
谢谢!
从 API.
获取 mainListView 项目的 MainActivity
private void callFilterItems()
{
final RequestParams requestParams = new RequestParams();
pref = getApplicationContext().getSharedPreferences("MyPref", Context.MODE_PRIVATE);
casevalue = pref.getString("category", getResources().getString(R.string.all));
requestParams.put("CategoryURL", casevalue);
final String uploadWebsite = getResources().getString(R.string.allitemlink);
AsyncHttpClient client = new AsyncHttpClient();
client.post(uploadWebsite, requestParams, new JsonHttpResponseHandler()
{
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response)
{
listofitemBylist.clear();
try
{
details = response.getJSONArray(TAG_DETAIL);
for (int i = 0; i < details.length(); i++)
{
JSONObject c = details.getJSONObject(i);
Log.e("Value","Of C"+c);
iTemName = c.getString(TAG_ITEM_NAME);
for (int j = 0; j < dbe.getCountValue(); j++)
{
text = dbe.getsubmenuitemnamechecked().get(j).getmenuID();
subMenuItemtext = sb.append(text+", ");
listofitems = new itemDetails(iTemName, subMenuItemtext .toString());
listofitemByList.add(listofitems);
}
}
catch (JSONException e)
{
e.printStackTrace();
}
pDialog.dismiss();
adapter = new ItemListAdapter(getApplicationContext(), R.layout.list_row, listofitemByList);
list.setAdapter(adapter);
}
我的自定义适配器:
public class ItemListAdapter extends ArrayAdapter<itemDetails>
{
ArrayList<ItemDetails> itemList;
private ArrayList<ItemDetails> originalList;
private LayoutInflater vi;
private Context context;
private SharedPreferences pref;
private String filtername;
private String text;
private int count;
public ItemListAdapter(Context context, int textViewResourceId, ArrayList<ItemDetails> nameList)
{
super(context, textViewResourceId, nameList);
this.itemList = new ArrayList<ItemDetails>();
this.itemList.addAll(nameList);
this.originalList = new ArrayList<ItemDetails>();
this.originalList.addAll(nameList);
this.context = context;
vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
private class ViewHolder
{
public TextView itemName;
public TextView itemsubtext;
}
@Override
public Filter getFilter()
{
if (filter == null)
{
filter = new FriendsFilter();
}
return filter;
}
@Override
public View getView(int position, View convertView, final ViewGroup parent)
{
ViewHolder holder = null;
if (convertView == null)
{
convertView = vi.inflate(R.layout.list_row, null);
holder = new ViewHolder();
holder.itemName = (TextView) convertView.findViewById(R.id.itemListName);
holder.itemsubtext = (TextView) convertView.findViewById(R.id.itemListsubName);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
FilterDetails pd = itemList.get(position);
holder.filterName.setText(pd.getfilterName());
holder.filterenabled.setText(pd.getsubItem());
return convertView;
}
我可以告诉你逻辑,你得自己去实现。使用哈希图。您在 hashmap 中的键(字符串)将是主列表视图的标题(日常使用、蔬菜等)。对应字符串的值将是包含 child 项的 Arraylist。
根据图片,Hashmap 中的键将是 "veggies",相应的 Arraylist 将包含选中的项目("potato","tomato" 等)。
您不需要在主列表视图中为不同的项目设置多个文本视图
前任-
Textview1 - 马铃薯
Textview2 - 番茄
这种方法没有任何优势。
只需为子项(Arraylist 项)创建一个文本视图并将它们的值设置为该文本视图
Textview t;
然后在某处初始化它
t.setText("tomato"
+","
+"potato"
+","
+"next item in corresponding arraylist");
我想用 select 在不同 activity 中编辑的项目更新子项目文本。这个怎么解决?
实际上这就是我想要做的:
我有一个名为 MainListview 的列表视图,具有不同的标题(日常使用、蔬菜、汽车用品)。我正在尝试根据子项 activity
中的项 selected 设置子项文本与图片相似的内容:
如果我点击 Veggies(另一个 activity with Listview 并在那里打开了复选框,我 select tomato and Potatoe 然后点击 OK。我现在应该在子项目中得到 tomato, Potatoe子项显示相同的值 Totmato、Potatoe
如何实现以上逻辑?
有人可以帮我解决这个问题吗?
现在:我正在做这样的事情:
listofitems = new itemDetails(iTemName, subitemtext.toString());
listofitemByList.add(listofitems);
adapter = new ItemListAdapter(getApplicationContext(), R.layout.list_row, listofitemByList);
list.setAdapter(adapter);
我在 MainListView 中得到以下错误结果:
谢谢!
从 API.
获取 mainListView 项目的 MainActivityprivate void callFilterItems()
{
final RequestParams requestParams = new RequestParams();
pref = getApplicationContext().getSharedPreferences("MyPref", Context.MODE_PRIVATE);
casevalue = pref.getString("category", getResources().getString(R.string.all));
requestParams.put("CategoryURL", casevalue);
final String uploadWebsite = getResources().getString(R.string.allitemlink);
AsyncHttpClient client = new AsyncHttpClient();
client.post(uploadWebsite, requestParams, new JsonHttpResponseHandler()
{
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response)
{
listofitemBylist.clear();
try
{
details = response.getJSONArray(TAG_DETAIL);
for (int i = 0; i < details.length(); i++)
{
JSONObject c = details.getJSONObject(i);
Log.e("Value","Of C"+c);
iTemName = c.getString(TAG_ITEM_NAME);
for (int j = 0; j < dbe.getCountValue(); j++)
{
text = dbe.getsubmenuitemnamechecked().get(j).getmenuID();
subMenuItemtext = sb.append(text+", ");
listofitems = new itemDetails(iTemName, subMenuItemtext .toString());
listofitemByList.add(listofitems);
}
}
catch (JSONException e)
{
e.printStackTrace();
}
pDialog.dismiss();
adapter = new ItemListAdapter(getApplicationContext(), R.layout.list_row, listofitemByList);
list.setAdapter(adapter);
}
我的自定义适配器:
public class ItemListAdapter extends ArrayAdapter<itemDetails>
{
ArrayList<ItemDetails> itemList;
private ArrayList<ItemDetails> originalList;
private LayoutInflater vi;
private Context context;
private SharedPreferences pref;
private String filtername;
private String text;
private int count;
public ItemListAdapter(Context context, int textViewResourceId, ArrayList<ItemDetails> nameList)
{
super(context, textViewResourceId, nameList);
this.itemList = new ArrayList<ItemDetails>();
this.itemList.addAll(nameList);
this.originalList = new ArrayList<ItemDetails>();
this.originalList.addAll(nameList);
this.context = context;
vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
private class ViewHolder
{
public TextView itemName;
public TextView itemsubtext;
}
@Override
public Filter getFilter()
{
if (filter == null)
{
filter = new FriendsFilter();
}
return filter;
}
@Override
public View getView(int position, View convertView, final ViewGroup parent)
{
ViewHolder holder = null;
if (convertView == null)
{
convertView = vi.inflate(R.layout.list_row, null);
holder = new ViewHolder();
holder.itemName = (TextView) convertView.findViewById(R.id.itemListName);
holder.itemsubtext = (TextView) convertView.findViewById(R.id.itemListsubName);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
FilterDetails pd = itemList.get(position);
holder.filterName.setText(pd.getfilterName());
holder.filterenabled.setText(pd.getsubItem());
return convertView;
}
我可以告诉你逻辑,你得自己去实现。使用哈希图。您在 hashmap 中的键(字符串)将是主列表视图的标题(日常使用、蔬菜等)。对应字符串的值将是包含 child 项的 Arraylist。 根据图片,Hashmap 中的键将是 "veggies",相应的 Arraylist 将包含选中的项目("potato","tomato" 等)。 您不需要在主列表视图中为不同的项目设置多个文本视图 前任- Textview1 - 马铃薯 Textview2 - 番茄 这种方法没有任何优势。
只需为子项(Arraylist 项)创建一个文本视图并将它们的值设置为该文本视图
Textview t;
然后在某处初始化它
t.setText("tomato"
+","
+"potato"
+","
+"next item in corresponding arraylist");