Listview doesnt fire onItemClickListener Event
i want to place onItemClick listener on my listview...but when i click on listview nothing is happen.. here is my code...can anyone suggest me how can i do this..??
Here is my XML FILE (list.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="@drawable/listbg">
<!-- For the Header Part -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="35dip"
android:background="#EE7621">
<Button
android:id="@+id/menuButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/backhome"
>
</Button>
<TextView
android:text="Pantries"
android:layout_height="wrap_content"
android:layout_width="210dip"
android:gravity="center"
android:shadowColor="#3B3B3B"
android:textColor="#FFFFFF"
android:textSize="20dip"
android:textStyle="bold">
</TextView>
<Button
android:id="@+id/allonmap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/allonmap"
>
</Button>
</LinearLayout>
<!-- For the List View -->
<TextView android:text="Total Pantries"
android:id="@+id/totalPantries"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#F2F2F2"
android:background="#B3B3B3">
</TextView>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<ListView
android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<!-- For the Footer Part -->
<RelativeLayout
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_centerHorizontal="true"
android:gravity="bottom">
<ImageView
android:id="@+id/footerview"
android:src="@drawable/footer"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_gravity="center">
</ImageView>
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
here is my Row.xml file
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<RelativeLayout
android:id="@+id/mainContent"
android:orientation="vertical"
android:layout_width="300dip"
android:layout_height="fill_parent">
<TextView
android:id="@+id/pantryName"
android:layout_marginTop="8dip"
android:layout_marginLeft="10dip"
android:text="name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="16dip"
android:textColor="#00AF33"
android:gravity="center_vertical"
/>
<TextView
android:layout_below="@id/pantryName"
android:textColor="#242424"
android:text="address1"
android:layout_marginTop="8dip"
android:layout_marginLeft="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/pantryAddress1"
/>
<TextView
android:layout_below="@id/pantryAddress1"
android:textColor="#242424"
android:text="address2"
android:layout_marginLeft="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/pantryAddress2"
/>
<Button android:layout_below="@+id/pantryAddress2"
android:layout_width="wrap_content"
android:id="@+id/phbtn"
android:layout_marginLeft="10dip"
android:layout_height="wrap_content"
android:background="@drawable/phonecall"
></Button>
<TextView
android:layout_toRightOf="@id/phbtn"
android:layout_width="fill_parent"
android:layout_below="@id/pantryAddress2"
android:textColor="#242424"
android:layout_marginLeft="10dip"
android:layout_height="wrap_content"
android:id="@+id/pantryPhno"
/>
<TextView
android:layout_below="@id/pantryPhno"
android:textColor="#242424"
android:textSize="10dip"
android:layout_marginLeft="12dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/distTime"
/>
</RelativeLayout>
<Button android:layout_width="wrap_content"
android:background="@drawable/next"
android:layout_marginTop="45dip"
android:layout_marginRight="7dip"
android:layout_alignParentRight="true"
android:id="@+id/nextBtn"
android:layout_height="wrap_content"
></Button>
</RelativeLayout>
here is my java file....
package com.pantylocator;
import java.net.URL;
import java.util.ArrayList;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class PantriesListActivity extends ListActivity
{
PantreisInfo parsedPantry;
ImageAdapter m_adapter;
TextView totalPantries;
public Button allOnMap,pantrySelection;
public static ArrayList<PantreisInfo> m_pantries=new ArrayList<PantreisInfo>();
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.m_adapter=new ImageAdapter(this, R.layout.row, m_pantries);
setListAdapter(this.m_adapter);
setContentView(R.layout.list);
ListView lv = getListView();
lv.setTextFilterEnabled(true);
Bundle bundle = this.getIntent().getExtras();
String parseURL = bundle.getString("url");
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view,int Position,long id)
{
Toast.makeText(getApplicationContext(),"Clicked",Toast.LENGTH_LONG).show();
}
});
parseURL(parseURL);
parsedPantry=PanatriesHandler.getPantries();
PantreisInfo pantry[]=null;
pantry =new PantreisInfo[parsedPantry.getID().size()];
for(int i=0;i<parsedPantry.getID().size();i++)
{
String temp_id=parsedPantry.getID().get(i);
String temp_name=parsedPantry.getName().get(i);
String temp_add1=parsedPantry.getAddress1().get(i);
String temp_add2=parsedPantry.getAddress2().get(i);
String temp_phno=parsedPantry.getPhone().get(i);
String temp_distTime=parsedPantry.getDistTime().get(i);
String temp_lat=parsedPantry.getGeo_lat().get(i);
String temp_long=parsedPantry.getGeo_long().get(i);
pantry[i]=new PantreisInfo();
pantry[i].setId(temp_id);
pantry[i].setName(temp_name);
pantry[i].setAddress1(temp_add1);
pantry[i].setAddress2(temp_add2);
pantry[i].setPhone(temp_phno);
pantry[i].setDistTime(temp_distTime开发者_Go百科);
pantry[i].setGeoLat(temp_lat);
pantry[i].setGeoLong(temp_long);
m_pantries.add(pantry[i]);
}
totalPantries=(TextView)findViewById(R.id.totalPantries);
totalPantries.setText("Total "+parsedPantry.getID().size()+" Pantries");
allOnMap=(Button)findViewById(R.id.allonmap);
allOnMap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
Toast.makeText(PantriesListActivity.this,
"Wait..map is being loaded",Toast.LENGTH_SHORT).show();
Intent myIntent;
myIntent=new Intent(PantriesListActivity.this, PantriesOnMap.class);
startActivityForResult(myIntent, 0);
// TODO Auto-generated method stub
}
});
}
void parseURL(String url)
{
try
{
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
URL sourceUrl = new URL(url);
PanatriesHandler pantriesHandler=new PanatriesHandler();
xr.setContentHandler(pantriesHandler);
xr.parse(new InputSource(sourceUrl.openStream()));
}
catch(Exception e)
{
System.out.println("==================>Exception Raised==>"+e);
}
}
class ImageAdapter extends ArrayAdapter<PantreisInfo>
{
private ArrayList<PantreisInfo> items;
public ImageAdapter(Context context,
int textViewResourceId,ArrayList<PantreisInfo> items)
{
super(context, textViewResourceId, items);
this.items = items;
}
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
if (v == null)
{
try
{
LayoutInflater vi = (LayoutInflater)
getSystemServiceContext.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
catch(Exception e)
{
System.out.println("Excption Caught"+e);
}
}
PantreisInfo p = items.get(position);
if (p != null)
{
TextView name = (TextView) v.findViewById(R.id.pantryName);
TextView add1= (TextView) v.findViewById(R.id.pantryAddress1);
TextView add2=(TextView)v.findViewById(R.id.pantryAddress2);
TextView phno = (TextView) v.findViewById(R.id.pantryPhno);
TextView distTime = (TextView) v.findViewById(R.id.distTime);
if (name != null)
{
name.setText(p.getName().get(0).toString());
}
if (add1 != null)
{
add1.setText(p.getAddress1().get(0).toString());
}
if (add2 != null)
{
add2.setText(p.getAddress2().get(0).toString());
}
if (phno != null)
{
phno.setText(p.getPhone().get(0).toString());
}
if (distTime!= null)
{
distTime.setText("Distribution Time: "+p.getDistTime().get(0).toString());
}
}
return v;
}
}
}
thanks in advance.....
hi nirav to get a onItem click of list u have to write onclickListener that i not found in your code try with that.
Instead of setting the clikclistener lv.setOnItemClickListener(new OnItemClickListener()
, can you use the override the method,
protected void onListItemClick(ListView l, View v, int position, long id)
which is provided by the ListActivity
try this, its working for me..
public void onItemClick(AdapterView<?> parent,
View view1, int position, long id) {
// The action to be taken when item is clicked
}
SimpleAdapter adapter = new SimpleAdapter(this,
groupData,
R.layout.listitem,
new String[] {"name" , "id"},
new int[]{ R.id.txtItemName, R.id.txtid} );
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3){
//u get positon of list item from arg2
}
});
精彩评论