Reputation: 1124
I'm have made a tab activity which works fine when i click on specific tab i extend the list activity and am also getting the list view but i'm not able to make the items in list clickable. code:
package com.infra.android.views;
import java.util.ArrayList;
import java.util.HashMap;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class TopNewsActivity extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
String xml = XMLfunctions.getTopNewsXML();
Document doc = XMLfunctions.XMLfromString(xml);
int numResults = XMLfunctions.numResults(doc);
if((numResults <= 0)){
Toast.makeText(TopNewsActivity.this, "No Result Found", Toast.LENGTH_LONG).show();
finish();
}
NodeList nodes = doc.getElementsByTagName("result");
for (int i = 0; i < nodes.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element)nodes.item(i);
map.put("id", XMLfunctions.getValue(e, "id"));
map.put("title", XMLfunctions.getValue(e, "title"));
mylist.add(map);
}
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main, new String[] { "title"}, new int[] { R.id.item_title});
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
@SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
Toast.makeText(TopNewsActivity.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_LONG).show();
}
});
}
}
when i click on the item it should probably give the id of the clicked item but it is not working.
Upvotes: 2
Views: 13989
Reputation: 33258
Try this:
lvlList = (ListView)findViewById(R.id.lvlList);
lvlList.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> a, View v,int position, long id)
{
Toast.makeText(getBaseContext(), "Click", Toast.LENGTH_LONG).show();
}
});
You have missed the @Override
.
Upvotes: 1
Reputation: 1530
I battled with this for some time before I figured out that areAllItemsEnabled
and isEnabled
in my costum ListAdapter
where set to false.
Upvotes: 0
Reputation: 19220
The problem is that in your adapter you inflate the main layout for every list item. That should be changed to an itemrenderer's layout.
Since you already have the solution for this question here, you should mark it solved either by accepting any of these answers, or by answering it yourself with the above link.
Upvotes: 0
Reputation:
u can use
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
}
Upvotes: 3
Reputation: 6235
There are problems in android with showing dialogs and toasts in subactivities, so your code have to work fine but to make toast try this:
Toast.makeText(TopNewsActivity.this.getParent(), "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_LONG).show();
Upvotes: 0
Reputation: 710
For ListView,
ListView lv = (ListView) findViewById(R.id.ContainlistItems);
lv.setItemsCanFocus(false);
and In XML file, make sure that for Textview inside list item set focusable false
android:focusable="false"
android:focusableInTouchMode="false"
Upvotes: 5