TheDevMan
TheDevMan

Reputation: 5954

Update sub item in ListView according to item selected in Android

I would like to update the subItem text with the item selected in different activity. How can this is solved?

Actually here is what I am trying to do:

I have listView called MainListview with different Headings (daily use, Veggies, car Stuff). I am trying to set subitem text according to the item selected in subItem activity

Something similar to picture:

enter image description here

If I click Veggies (Another activity with Listview and check box is opened there I select tomato and Potatoe and click OK. I should get tomato, Potatoe in subitem instead right now all the sub item shows the same values Totmato, Potatoe

how can achieve the above logic?

Can somebody help me with this?

Right now: I am doing soemthing like this:

 listofitems = new itemDetails(iTemName, subitemtext.toString());
 listofitemByList.add(listofitems);

 adapter = new ItemListAdapter(getApplicationContext(), R.layout.list_row, listofitemByList);
 list.setAdapter(adapter);

And I get the following wrong results in MainListView:

enter image description here

Thanks!

MainActivity which gets the mainListView Items from API.

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);

    }

My CUSTOM 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;
}

Upvotes: 0

Views: 847

Answers (1)

Umesh Chhabra
Umesh Chhabra

Reputation: 278

I can tell you the logic, you have to implement it yourself. Use Hashmap. Your key(string) in hashmap will be main listview's heading (daily use, veggies etc). Value to corresponding string will be Arraylist that will contain child items. According to pics key in Hashmap will be "veggies" and corresponding Arraylist will contain checked items ("potato" ,"tomato" etc). You do not need to have multiple textviews in Main listview for different items Ex- Textview1 - potato Textview2 - tomato This approach do not have any advantage.

Just make One textview for subitems(Arraylist items) and set their values to that textview

Textview t;

Initialize it somewhere then

t.setText("tomato"
+","
+"potato"
+","
+"next item in corresponding arraylist");

Upvotes: 1

Related Questions