Android ListView not appearing - android

My listview doesnt seem to show on screen.
I'm using a tabhost and put a list in a frame but for some reason it's not working.
I'm sure my code seems right so am unsure what's wrong here..
Help please!
Here's my XML:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#android:id/tabhost">
<LinearLayout
android:id="#+id/LinearLayout01"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="List"
android:id="#+id/listtext"
android:layout_gravity="center_horizontal" />
<TabWidget
android:id="#android:id/tabs"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<ListView
android:id="#+id/list"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="1">
</ListView>
</FrameLayout>
</LinearLayout>
</TabHost>
Tab 1 Code:
public class WorkoutDay1 extends Activity {
ListView listView ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.workoutlist_main);
// Get ListView object from xml
listView = (ListView) findViewById(R.id.list);
// Defined Array values to show in ListView
String[] values = new String[] { "Chest Press",
"Shoulder Press",
"Arm Extension",
};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
// Assign adapter to ListView
listView.setAdapter(adapter);
// ListView Item Click Listener
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
/* // ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) listView.getItemAtPosition(position);*/
// Show Alert
if(position == 0) {
//code specific to first list item
Intent myIntent = new Intent(view.getContext(), workout.class);
startActivityForResult(myIntent, 0);
}
if(position == 1) {
//code specific to 2nd list item
Toast.makeText(getApplicationContext(),
"Coming Soon.", Toast.LENGTH_SHORT)
.show();
}
if(position == 2) {
//code specific to 2nd list item
Toast.makeText(getApplicationContext(),
"Coming Soon." , Toast.LENGTH_SHORT)
.show();
}
}
});
}
}

You hvnt posted your complete code but I am just guessing that you might not have called notifyDataSetChanged() after assigning data to your list..

Related

Android onItemClick method not working for item of ids other than 0

I have a problem with onItemClick method in android.
I know other people have encountered such problems, found some references to their questions on SO, read about the answers provided and some other articles on the internet but it didn't help. Things seem a bit different in my case.
I have a listview of some objects for which I want to show details when they're clicked. But Awkwardly the listener is working fine for only the first object of the listview. The app keeps aborting when I click on other elements of the listview. I can't figure out what's going on.
As I was using the id to retrieve the object I tried changing the id by the position, which didn't work either.
Here is the code of the onCreate method in my activity (DrinkCategoryActivity)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drink_category);
ArrayAdapter<Drink> listAdapter = new ArrayAdapter<>(
this, android.R.layout.simple_list_item_1, Drink.drinks
);
ListView listDrinks = (ListView) findViewById(R.id.list_drinks);
listDrinks.setAdapter(listAdapter);
AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> listDrinks,
View itemView, int position, long id) {
Intent intent = new Intent(DrinkCategoryActivity.this, DrinkActivity.class);
intent.putExtra(DrinkActivity.EXTRA_DRINKID, (int) id);
startActivity(intent);
}
};
listDrinks.setOnItemClickListener(itemClickListener);
}
My DrinkActivity (the one that should show me the details of each Drink object when clicked in the listview) onCreate method code is shown below
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drink);
int drinkId = (int) getIntent().getExtras().get(EXTRA_DRINKID);
Drink drinkItem = Drink.drinks[drinkId];
ImageView image = (ImageView) findViewById(R.id.drink_image);
image.setImageResource(drinkItem.getImageResourceId());
TextView drink_name = (TextView) findViewById(R.id.drink_name);
drink_name.setText(drinkItem.getName());
TextView drink_description = (TextView) findViewById(R.id.drink_description);
drink_description.setText(drinkItem.getDescription());
TextView drink_price = (TextView) findViewById(R.id.drink_price);
String price = ""+drinkItem.getPrice();
drink_price.setText(price);
}
I don't know whether there is something wrong with the xml files so here they are :
activity_drink_category.xml goes below
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.DrinkCategoryActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
layout="#layout/customer_screen_toolbar_layout"/>
<ListView
android:layout_marginTop="10dp"
android:id="#+id/list_drinks"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</androidx.drawerlayout.widget.DrawerLayout>
And activity_drink.xml goes here
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".activities.DrinkActivity">
<include
layout="#layout/customer_screen_toolbar_layout"/>
<ImageView
android:id="#+id/drink_image"
android:layout_width="190dp"
android:layout_height="190dp" />
<TextView
android:id="#+id/drink_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/drink_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/drink_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#EB8E13"
android:textColor="#color/white" />
</LinearLayout>
Any help will be valuable. Thanks in advance
You need to pass "Position" instead of "id" in Intent. As you describe above its works fine for the first position. Then you need to Use position
intent.putExtra(DrinkActivity.EXTRA_DRINKID, (int) position);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drink_category);
ArrayAdapter<Drink> listAdapter = new ArrayAdapter<>(
this, android.R.layout.simple_list_item_1, Drink.drinks
);
ListView listDrinks = (ListView) findViewById(R.id.list_drinks);
listDrinks.setAdapter(listAdapter);
AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> listDrinks,
View itemView, int position, long id) {
Intent intent = new Intent(DrinkCategoryActivity.this, DrinkActivity.class);
intent.putExtra(DrinkActivity.EXTRA_DRINKID, (int) position);
startActivity(intent);
}
};
listDrinks.setOnItemClickListener(itemClickListener);
}

How to not display all list elements at one time in android?

I have a listview in Fragment, which includes some list elements.
Here is the xml-layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context=".uiFragments.WikiFragment">
<LinearLayout
android:id="#+id/tablayout"
android:layout_below="#+id/wiki_linear_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#+id/list"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginTop="5dp"/>
<com.github.barteksc.pdfviewer.PDFView
android:id="#+id/pdfView"
android:layout_below="#+id/tv_header"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"/>
</LinearLayout>
</FrameLayout>
here is a part of the code from this fragment:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_wiki, container, false);
//ListView
listView = (ListView) view.findViewById(R.id.list);
// Defined Array values to show in ListView
String[] values = new String[] { "Android List View",
"Adapter implementation",
"Simple List View In Android",
"Create List View Android",
};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, android.R.id.text1, values);
// Assign adapter to ListView
listView.setAdapter(adapter);
// ListView Item Click Listener
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) listView.getItemAtPosition(position);
// Show Alert
Toast.makeText(getContext().getApplicationContext(),
"Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG)
.show();
}
return view;
});
What I would like to have is like in the picture:
At the moment, all my list-items will be displayed.
But I want to display for example only 2 list-items and the rest shall be scrollable choosen, like in the image above.
This is what I get now.
If someone could me help out, I would be very happy. Thanks

how set the text of textview with listitem when the listitem is clicked

I want to set the text of textview with listitem when a list item is clicked.
i have list of items. when i click on an item it displays the html page. i have layout to display the HTML page(Using WebView), heading(TextView) and back button(Imagebutton).i am able to display the html page in webview without any problem. Problem is with the heading. when i click on list item of list. i want the same item to be set as heading in the textview. Please suggest me. i am able to toast the listitem, but i do not need it. i want to set in text of textview.please see the below code.
Topics.java
public class Topics extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// storing string resources into Array
String[] topic_sections = getResources().getStringArray(R.array.topic_sections);
// Binding resources Array to ListAdapter
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.label, topic_sections));
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
change(position);
}
void change(int position){
switch(position){
case 0 :{
Intent intent = new Intent(getApplicationContext(), TopicsDisplay.class);
intent.setData(Uri.parse("file:///android_asset/home.html"));
startActivity(intent);
}
break;
case 1 :{
Intent intent = new Intent(getApplicationContext(), TopicsDisplay.class);
intent.setData(Uri.parse("file:///android_asset/program.html"));
startActivity(intent);}
break;
} } }
TopicsDisplay.java
public class TopicsDisplay extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.topic_display);
WebView webview1 = (WebView) findViewById(R.id.webView1);
webview1.loadUrl(getIntent().getDataString());
webview1.getSettings().setBuiltInZoomControls(true);
webview1.setInitialScale(1);
webview1.setPadding(0, 0, 0, 0);
}
public void finishActivity(View v){
finish();
}
}
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Single List Item Design -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/label"
android:textColor="#ea9999"
android:background="#000000"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="20sp"
android:textStyle="bold"
android:focusable="false"
android:clickable="false">
</TextView>
topic_display.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/myBackground">
<RelativeLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="42dp"
android:layout_height="42dp"
android:onClick="finishActivity"
android:contentDescription="#string/back"
android:src="#drawable/back_button" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="45dp"
android:textStyle="bold"
android:textColor="#000000"
android:gravity="center_vertical"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:paddingLeft="10dp"
android:id="#+id/header"
/>
</RelativeLayout>
<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/webView1"/>
</LinearLayout>
After getting the item from the list view how to set that listitem in the text of textview which is there in the topic_display layout.
My suggestion is to put the title you want to show in the Intent's extras.
It means, that when you call
Intent intent = new Intent(getApplicationContext(), TopicsDisplay.class);
intent.setData(Uri.parse("file:///android_asset/home.html"));
startActivity(intent);
you should put the title in the Intent's extra before calling the startActivity() method (in both cases you are calling the TopicsDisplay activity)
Intent intent = new Intent(getApplicationContext(), TopicsDisplay.class);
intent.setData(Uri.parse("file:///android_asset/home.html"));
intent.putExtra("title", topic_sections[position]);
startActivity(intent);
And then, in the TopicsDisplay activity you can get the title by calling
getIntent().getStringExtra("title")
Finally, you should end up with something like this in the TopicsDisplay.onCreate method:
TextView titleTV = (TextView) findViewById(R.id.header)
titleTV.setText(getIntent().getStringExtra("title"));
Hope this helps!

How to implement onItemClickListener in List Activity

following is my ListActivity, onCreate code
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fakeTripBuilder();
setContentView(R.layout.cutom_list_view);
SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.custom_row_view,
new String[] {"stop","distance","time","icon","mode"},
new int[] {R.id.text_stop,R.id.text_distance, R.id.text_time, R.id.icon, R.id.text_mode});
populateList();
setListAdapter(adapter);
}
inside the the same class i have the following method
#Override
protected void onListItemClick(ListView l, View v, int position, long id){
super.onListItemClick(l, v, position, id);
Log.d("Travel","You choosed!");
Object o = this.getListAdapter().getItem(position);
Toast.makeText(this, "You Choosed"+o.toString(), Toast.LENGTH_LONG).show();
}
and here is the "custom_list_view.xml"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000fff"
android:layout_weight="2"
android:drawSelectorOnTop="false">
</ListView>
<TextView
android:id="#id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFff00"
android:text="No data"
/>
</LinearLayout>
Problem is, onListItemClick method do not call when i click on it. Help !!
To get a reference to the ListView in a ListActivity you should call:
ListView lv = getListView();
You do not need to set a click listener, it already has one. Simply override:
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
Object item = lv.getAdapter().getItem(position);
}
And in your xml file, the ListView should have the id:
android:id="#android:id/list"
Edit
You need to remove
android:clickable="false"
from the layout to allow any child views to be clicked
A lot has changed since this question was asked in 2012. This is an update to the accepted answer
To use onItemClickListener in your ListActivity, just override the onListItemClick(...) method like this:
public void onListItemClick(ListView listView, View view, int position, long id) {
ListView myListView = getListView();
Object itemClicked = myListView.getAdapter().getItem(position);
Toast.makeText(getApplicationContext(), "Item Clicked: " + item + " \nPosition: " + id,
Toast.LENGTH_SHORT).show();
}
Notice the getItem() method instead of getItemAtPosition() in the accepted answer
Remember: for this to work, you need to have a listview with id list, else your app will crash.
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/list"
/>
Try overriding the onListItemClick method if you want to catch click events on your list.
ListView lv = (ListView) findViewById(R.layout.cutom_list_view);
You should use R.id.listview_id instead, that should be defined in R.layout.cutom_list_view
like so:
<ListView
android:id="#+id/listview_id"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
EDIT:
R.layout.cutom_list_view is id of your layout file, but you should find listview by its own id, defined like android:id="#+id/listview_id", so the id will be R.id.listview_id

Not able to get the clicked row number in list view of android

I am trying to build a simple sms app for which edit page lists all the saved smses. I gave 2 buttons in my layout for Edit and Delete but when I am clicking on them I am unable to get the rowid (row number from top) to retrieve the sms corresponding to that particular row.. Somebody please help me..my sms adapter is not not extending anything..Here's my class and xml files..
public class SMSEdit extends ListActivity{
private SMSDbAdapter mDbHelper;
//private Button editsmsbtn, deletesmsbtn;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.editsmslist);
mDbHelper = new SMSDbAdapter(this);
mDbHelper.open();
fillData();
}
public void fillData() {
Cursor smsCursor = mDbHelper.fetchAllNotes();
startManagingCursor(smsCursor);
// Create an array to specify the fields we want to display in the list (only TITLE)
String[] from = new String[]{SMSDbAdapter.KEY_TITLE,
SMSDbAdapter.KEY_BODY,
SMSDbAdapter.KEY_DATE,
SMSDbAdapter.KEY_TIME};
// and an array of the fields we want to bind those fields to (in this case just text1)
int[] to = new int[]{R.id.smseditlistTO,
R.id.smseditlistMessage,
R.id.smseditlistDay,
R.id.smseditlistTime};
// Now create a simple cursor adapter and set it to display
SimpleCursorAdapter smsadap =
new SimpleCursorAdapter(this, R.layout.smsrow, smsCursor, from, to);
setListAdapter(smsadap);
}
public void myEditClickHandler(View v)
{
//get the row the clicked button is in
LinearLayout vwParentRow = (LinearLayout)v.getParent();
Button btnChild = (Button)vwParentRow.getChildAt(4);
btnChild.setText("I've been clicked!");
ListAdapter pos = getListAdapter();
Intent intent = new Intent();
intent.setClass(SMSEdit.this, SMS.class);
// intent.putExtra(SMSDbAdapter.KEY_ROWID, id);
startActivity(intent);
finish();
}
public void myDeleteClickHandler(View v)
{
//get the row the clicked button is in
LinearLayout vwParentRow = (LinearLayout)v.getParent();
//TextView child = (TextView)vwParentRow.getChildAt(0);
Button btnChild = (Button)vwParentRow.getChildAt(5);
//btnChild.setText(child.getText());
btnChild.setText("I've been clicked!");
int c = Color.CYAN;
vwParentRow.setBackgroundColor(c);
vwParentRow.refreshDrawableState();
}
}
editsmslist.xml -- >>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ListView android:id="#+id/android:list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/android:empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No Future SMS Yet" android:textSize="35px"/>
</LinearLayout>
smsrow.xml -- >>
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:id="#+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:id="#+id/LinearLayout02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout android:id="#+id/LinearLayout03" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="#string/smsdetailseditlist" android:id="#+id/smseditlistTO" android:paddingLeft="5px">
</TextView>
<TextView android:layout_height="wrap_content" android:text="#string/smsdetailseditlist" android:id="#+id/smseditlistMessage" android:paddingLeft="20px" android:maxLines="1" android:layout_width="wrap_content">
</TextView>
</LinearLayout>
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" android:paddingLeft="5px">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Date: "></TextView><TextView android:text="#string/smsdetailseditlist" android:id="#+id/smseditlistDay" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Time: " android:paddingLeft="5px"> </TextView><TextView android:text="#string/smsdetailseditlist" android:id="#+id/smseditlistTime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingRight="10px"></TextView>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/EditSMSButton" android:text="#string/editsms" android:onClick="#string/myEditClickHandler"></Button>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/DeleteSMSButton" android:text="#string/deletesms" android:onClick="#string/myDeleteClickHandler"></Button>
</LinearLayout>
</LinearLayout>
</ScrollView>
Also Please suggest if there'a yn better way to implement this or make the layout look better...
You can extend a cursorAdapter like this...
public class exampleAdapter extends CursorAdapter
{
LayoutInflater inflater;
public exampleAdapter (Context context, Cursor c) {
super(context, c);
inflater = LayoutInflater.from(context);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
// TODO Auto-generated method stub
Button btn_del = (Button)view.findViewById(R.id.button1);
btn_del.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
SMSDbAdapter vb = new SMSDbAdapter(context);
vb.open();
String xyz = cursor.getString(0); //Persumably your ID
vb.Delete(Integer.parseInt(cursor.getString(0)));
Toast.makeText(context, xyz + " Deleted!", Toast.LENGTH_SHORT).show();
vb.close();
}
});
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return inflater.inflate(R.layout.smsrow, parent, false);
}
}
I had a similar problem with my ListView.
Assume you are filling a ListView dynamically. You have 5 rows (for example). The id of first row is 1 second is 2 and so on.
When you scroll whatever rows are visible to you, the id of the 1st row (that you currently see on screen) will always be 1. You need to control that by using mabye some another class (XYZ).
I simply used ArrayList<XYZ> and that worked.
You can override this method in your SMSAdapter:
public View getView(int position, View convertView, ViewGroup parent)
and then call the get(position) method that returns an ArrayList<XYZ>.
You should use ListView to display your messages.
Store the data retrieved from database in a Array of Strings say s1.
Then
private ArrayList<String> arr_sort = new ArrayList<String>();
And copy whole s1 into arr_sort by using add() method.
While setting adapter
lv.setAdapter(new ArrayAdapter<String>(DataAttach.this,
android.R.layout.simple_list_item_1, arr_sort));
where lv is the ListView refernce.
Then use this method for onClick
lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String item = (String) lv.getItemAtPosition(position);
Toast.makeText(*.this,
"You selected - " + item + " ,Please wait...",
Toast.LENGTH_LONG).show();
Intent intent = new Intent(*.this, *.class);
*.this.startActivity(intent);
}
});
Hope this helps..

Categories

Resources