Skip to content

Instantly share code, notes, and snippets.

@yahyaahrika
Last active June 25, 2016 13:32
Show Gist options
  • Save yahyaahrika/956a35d4ddaa6e34bdb4819ce0645549 to your computer and use it in GitHub Desktop.
Save yahyaahrika/956a35d4ddaa6e34bdb4819ce0645549 to your computer and use it in GitHub Desktop.
ListView Get id title content
-------------------------- activity_main.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv=(ListView)findViewById(R.id.lv);
final ArrayList<List_item> Items=new ArrayList<List_item>();
Items.add(new List_item(8, "hell dfdvd 8o", "maroc pal in"));
Items.add(new List_item(5, "hello h5", "maroc pal in"));
Items.add(new List_item(9, "hello 9", "maroc dada"));
Items.add(new List_item(22, "hello 22", "maroc nadd in"));
myCastmerAdapter adapter=new myCastmerAdapter(Items);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
TextView title=(TextView) view.findViewById(R.id.Title);
TextView content=(TextView) view.findViewById(R.id.Content);
Toast.makeText(getBaseContext(),String.valueOf(Items.get(position)._id), Toast.LENGTH_SHORT).show();
}
});
class myCastmerAdapter extends BaseAdapter{
ArrayList<List_item> Item=new ArrayList<List_item>();
myCastmerAdapter(ArrayList<List_item> item){
this.Item=item;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return Item.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=getLayoutInflater();
View view1=inflater.inflate(R.layout.row_listview, null);
TextView title=(TextView) view1.findViewById(R.id.Title);
TextView content=(TextView) view1.findViewById(R.id.Content);
title.setText(Item.get(position).Title);
content.setText(Item.get(position).Content);
return view1;
}
}
--------------------------------------------------- List_item
package com.example.t01_listview_id;
public class List_item {
int _id;
String Title;
String Content;
public List_item(int _id,String title,String content) {
this._id=_id;
this.Title=title;
this.Content=content;
}
}
--------------------------------------------------- row_listview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/Content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</LinearLayout>
------------------------------------- main
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.t01_listview_id.MainActivity" >
<ListView
android:id="@+id/lv"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
</ListView>
</RelativeLayout>
@yahyaahrika
Copy link
Author

ujuj

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment