将列表视图连同文本和 onitemclick 意图填充到包含另一个列表视图以及基于选择的文本的新布局

Populate listview along with text and onitemclick intent to new layout containing another listview along with text based on the selection

我需要一个包含城市及其面积的列表视图(即) 班加罗尔(10) 德里(5) 孟买(15) 一个一个这样。 单击每个城市我需要另一个列表视图,其中包含这些区域以及该区域的商店数量..(即)如果我单击班加罗尔,它会显示 10 个区域的列表视图以及每个区域的商店数量....(例如) area1(10) area2(20) area3(25) 像这样..

help me with the sample code to display (listview and text) onitemclick on another (listview and text)....and also to display the text with parenthesis () Example:: Bangalore(10)

我附上了我的代码:

TextListViewActivity::

public class TextListViewActivity extends Activity implements
        OnItemClickListener {
    int chn_len,mum_len,cal_len;
    String[]  city;
    String[] descriptions;

    ListView listView1;
    List<RowItem> rowItems1;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        city = getResources().getStringArray(R.array.city_array);
        chn_len=getResources().getStringArray(R.array.area_chennai).length;
        mum_len=getResources().getStringArray(R.array.area_mumbai).length;
        cal_len=getResources().getStringArray(R.array.area_calcutta).length;
         descriptions = new String[] {String.valueOf(chn_len),String.valueOf(mum_len),String.valueOf(cal_len)};


        rowItems1 = new ArrayList<RowItem>();
        for (int i = 0; i < city.length; i++) {
            RowItem item = new RowItem(city[i], descriptions[i]);
            rowItems1.add(item);
        }

        listView1 = (ListView) findViewById(R.id.list);
        CustomListViewAdapter adapter1 = new CustomListViewAdapter(this,
                R.layout.list_item, rowItems1);
        listView1.setAdapter(adapter1);
        listView1.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
                            long id) {

    }
}

CustomListViewAdapter::

public class CustomListViewAdapter extends ArrayAdapter<RowItem> {

    Context context;

    public CustomListViewAdapter(Context context, int resourceId,
                                 List<RowItem> items) {
        super(context, resourceId, items);
        this.context = context;
    }

    /*private view holder class*/
    private class ViewHolder {
        //ImageView imageView;
        TextView txtTitle1;
        TextView txtDesc1;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        RowItem rowItem = getItem(position);

        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_item, null);
            holder = new ViewHolder();
            holder.txtDesc1 = (TextView) convertView.findViewById(R.id.desc);
            holder.txtTitle1 = (TextView) convertView.findViewById(R.id.title);
           // holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();

        holder.txtDesc1.setText(rowItem.getDesc());
        holder.txtTitle1.setText(rowItem.getTitle());
       // holder.imageView.setImageResource(rowItem.getImageId());

        return convertView;
    }
}

行项:

public class RowItem {
   // private int imageId;
    private String title;
    private String desc;

    public RowItem(String title, String desc) {
      //  this.imageId = imageId;
        this.title = title;
        this.desc = desc;
    }
  /*  public int getImageId() {
        return imageId;
    }
    public void setImageId(int imageId) {
        this.imageId = imageId;
    }*/
    public String getDesc() {
        return desc;
    }
    public void setDesc(String desc1) {
        this.desc = desc1;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title1) {
        this.title = title;
    }
    @Override
    public String toString() {
        return title + "\n" + desc;
    }


}

TextListViewActivity::

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
                            long id) {

            Bundle bundle = new Bundle();
            bundle.putSerializable("object", rowItems1.get(position));
            Intent intent = new Intent(TextListViewActivity .this,TextListViewDetailActivity.class);
            intent.putExtra("bundle",bundle);
            startActivity(intent);
    }

TextListViewDetailActivity::

从意图中获取城市数据,如下所示。

    Bundle bundle = getIntent().getBundleExtra("bundle");
    if (bundle != null) {
        RowItem model = (RowItem) bundle.getSerializable("object");
    }

在模型中,您将拥有城市数据。使用它来获取区域数据并将其填充到列表视图中。

行项:

public class RowItem implements Serializable  {
....
}

只需将 implements Serializable 添加到您的 RowItem class。