I need a listview containing cities along with their area count (ie) Bangalore(10) Delhi(5) Mumbai(15) one by one likethis. Onclicking each cities i need a another listview containing those areas along with the count of shops in that area..(ie) If i click bangalore it displays listview of 10 areas along with count of shops in that each area....(ex) area1(10) area2(20) area3(25) likethis..
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)
I have attached my code :
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;
}
}
RowItem:
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::
get city data from intent as shown below.
Bundle bundle = getIntent().getBundleExtra("bundle");
if (bundle != null) {
RowItem model = (RowItem) bundle.getSerializable("object");
}
here in the model, you will have city data. Use it to fetch area data and populate it in the listview.
RowItem:
public class RowItem implements Serializable {
....
}
just add implements Serializable to your RowItem class.