我想使用在不同活动中选择的项目更新subItem文本。怎么能解决这个问题?
其实这就是我要做的事情:
我有一个名为MainListview的listView,带有不同的标题(日常使用,蔬菜,汽车用品)。我正在尝试根据subItem活动
中选择的项目设置子项目文本与图片相似的东西:
如果我点击Veggies(另一个带有Listview和复选框的活动在那里打开,我选择番茄和Potatoe,然后点击OK。我应该得到番茄,Potatoe在subitem而不是现在所有的子项显示相同的值Totmato,Potatoe < / p>
如何实现上述逻辑?
有人可以帮我这个吗?
现在:我正在这样做:
listofitems = new itemDetails(iTemName, subitemtext.toString());
listofitemByList.add(listofitems);
adapter = new ItemListAdapter(getApplicationContext(), R.layout.list_row, listofitemByList);
list.setAdapter(adapter);
我在MainListView中得到以下错误结果:
谢谢!
MainActivity从API获取mainListView项目。
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;
}
答案 0 :(得分:1)
我可以告诉你逻辑,你必须自己实现它。使用Hashmap。您在hashmap中的键(字符串)将是主列表视图的标题(日常使用,蔬菜等)。对应字符串的值将是包含子项的Arraylist。 根据pash键,Hashmap将是&#34; veggies&#34;和相应的Arraylist将包含选中的项目(&#34;马铃薯&#34;,&#34;番茄&#34;等)。 您不需要在主列表视图中为不同的项目提供多个文本视图 EX- Textview1 - 土豆 Textview2 - 番茄 这种方法没有任何优势。
只为子项目(Arraylist项目)创建一个textview并将其值设置为该textview
Textview t;
在某处初始化
t.setText("tomato"
+","
+"potato"
+","
+"next item in corresponding arraylist");