Reputation:
I am developing an app where I am using customized listview. First i will go with code and i will ask my question(at bottom).
Main.java (using adapter class to set data to listview)
Myadapter adapter = new Myadapter();
listview.setAdapter(adapter);
}
custom.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Large Text"
android:background="#adadad"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Myadapter class:
public class Myadapter extends BaseAdapter{
public int getCount() {
// TODO Auto-generated method stub
return startarr.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = null;
int i;
LayoutInflater layoutinflater = getLayoutInflater();
v = layoutinflater.inflate(R.layout.custom,null);
TextView aptdate = (TextView)v.findViewById(R.id.textView1);
TextView apttime = (TextView)v.findViewById(R.id.textView2);
TextView aptname = (TextView)v.findViewById(R.id.textView3);
TextView aptid = (TextView)v.findViewById(R.id.textView4);
//Button btn = (Button)v.findViewById(R.id.button1);
final String arlstdate[] = startarr.get(position).split("~");
for (i = 0; i < arlstdate.length; i++) {
aptdate.setText(arlstdate[i]);
try {
// Getting Array of Contacts
JSONObject json = new JSONObject(response);
contacts = json.getJSONArray("schedule");
// looping through All Contacts
for(int j = 0; j < contacts.length(); j++){
JSONObject c = contacts.getJSONObject(j);
// Storing each json item in variable
final String id = c.getString("scheduleId");
String startTime = c.getString("startDateTime");
String endTime = c.getString("endDateTime");
String type = c.getString("scheduleType");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date startedTime = sdf.parse(startTime);
Date endedTime = sdf.parse(endTime);
int getstartdate = startedTime.getDate();
int getstartmonth = startedTime.getMonth();
int getstartyear = startedTime.getYear();
int getday = startedTime.getDay();
final int getstartingtime = startedTime.getHours();
final int getstartingmin = startedTime.getMinutes();
long diff = endedTime.getTime() - startedTime.getTime();
int hours = (int)(diff/(60*60*1000));
testselectID = String.valueOf(hours);
hoursarray.add(testselectID);
starttimearray.add(String.valueOf(getstartingtime+":"+getstartingmin));
calendar = Calendar.getInstance();
calendar.setTime(startedTime);
System.out.println((calendar.get(Calendar.YEAR))+"-"+(calendar.get(Calendar.MONTH)+1)+"-"+(calendar.get(Calendar.DAY_OF_MONTH)));
if(arlstdate[i].equals((calendar.get(Calendar.YEAR))+"-"+(calendar.get(Calendar.MONTH)+1)+"-"+(calendar.get(Calendar.DAY_OF_MONTH))))
{
aptid.append(id+"\n");
apttime.append(testselectID+"Hrs"+"\n");
aptname.append((String.valueOf(getstartingtime+":"+getstartingmin))+"\n");
}
}
}catch (Exception e) {
// TODO: handle exception
Toast.makeText(getBaseContext(), "schedule error is " + e, Toast.LENGTH_SHORT).show();
}
}
return v;
}
}
Here I am appending the data to textviews and output of the following code is ...
\
Upto here everything works good. But what my question is.....
When i click on custom list view the values need to display in seperate toast message. As i have some idea regarding list view click listener, but here if using that code it is taking last appeneded data details. for example if i click on first one (in image) it is displaying 16:40.not displyaing 12:0. what i want is I need to display two of them differently with different toast message.
Can anyone help me with this....
Upvotes: 2
Views: 371
Reputation: 2755
for that you have to implement the OnItemClickListener event on listview .
listview.OnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
}
});
Now you have position of your listview then Whatever the list position that corresponding value is diplays in toast.
Upvotes: 0
Reputation: 15679
You make your live very hard yourself in NOT implementing a proper Model. You should parse your Json outside the Adapter and make Java-classes out of these informations. Then pass a List or an Array as data to your Adapter, while returning Model-objects in getItem(position)
!
This way you flawlessly can implement an OnItemClickListener
and gather whatever data you want.
You also just completely ignore the convertView
Parameter. You should take this View instead of inflating a new one if it is NOT null for performance optimization!
Upvotes: 2