Android:Listview onClick无法正常工作?
我有Listview,它的数据来自website.data。它在Listview中正确显示,但其唯一项Click未正确触发Android:Listview onClick无法正常工作?,android,Android,我有Listview,它的数据来自website.data。它在Listview中正确显示,但其唯一项Click未正确触发 public class OutKrys extends ListActivity { private ProgressDialog progress; ListView listView1; private ArrayList<Post> listItems = new ArrayList<Post>(); private PostAdapte
public class OutKrys extends ListActivity {
private ProgressDialog progress;
ListView listView1;
private ArrayList<Post> listItems = new ArrayList<Post>();
private PostAdapter post_Adapter;
public static DefaultHttpClient httpclient = new DefaultHttpClient();
private class PostAdapter extends ArrayAdapter<Post> {
private ArrayList<Post> items = new ArrayList<Post>();
public PostAdapter(Context context, int textViewResourceId,
ArrayList<Post> items) {
super(context, textViewResourceId, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
try {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.outkrys, null);
}
Post o = items.get(position);
if (o != null) {
TextView tt = (TextView) v.findViewById(R.id.Story);
TextView bt = (TextView) v.findViewById(R.id.userName);
TextView tm = (TextView) v.findViewById(R.id.Time);
ImageView iv=(ImageView)v.findViewById(R.id.OutKryImageView01);
if (tt != null) {
tt.setText(o.getStory());
}
if (bt != null) {
bt.setText(o.getUserName());
}
if (tm != null) {
tm.setText(o.getTime());
if(iv!=null)
{
Drawable drawable = LoadImageFromWebOperations("abc.com/"+o.getImagePath());
iv.setImageDrawable(drawable);
}
}
}
return v;
} catch (Exception e) {
progress.dismiss();
return null;
}
}
}
private Drawable LoadImageFromWebOperations(String url)
{
try
{
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
}catch (Exception e) {
System.out.println("Exc="+e);
return null;
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// Get the item that was clicked
Object o = this.getListAdapter().getItem(position);
String keyword = o.toString();
Toast.makeText(this, "You selected: " + position, Toast.LENGTH_LONG)
.show();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.listitems);
//listView1 = (ListView) findViewById(R.id.ListView01);
try {
progress = ProgressDialog.show(OutKrys.this, "Please wait...",
"Loading list", true);
Thread t = new Thread() {
public void run() {
listItems=getList();
pHandler.post(mUpdateResults);
}
};
t.start();
} catch (Exception e) {
//progress.dismiss();
Toast.makeText(OutKrys.this, "dsadsadsa"+e, Toast.LENGTH_LONG).show();
}
}
final Handler pHandler = new Handler();
// Create runnable for posting
final Runnable mUpdateResults = new Runnable() {
public void run() {
updateTabData();
}
};
private void updateTabData() {
try {
this.post_Adapter = new PostAdapter(this, android.R.layout.simple_list_item_checked,
listItems);
this.setListAdapter( new PostAdapter(this, R.layout.outkrys,
listItems));
progress.dismiss();
} catch (Exception e) {
//progress.dismiss();
}
}
private ArrayList<Post> getList() {
try {
String loginURI = "abcd.com";
HttpPost httpost = new HttpPost(loginURI);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("authenticate", "authenticate"));
nvps.add(new BasicNameValuePair("tab", "Recent"));
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
HttpResponse response = null;
response = KryAbout.httpclient.execute(httpost);
HttpEntity entity = response.getEntity();
Post p = new Post();
String line = "";
if (entity != null) {
InputStream resContent = response.getEntity().getContent();
line = KryAbout.ReadInputStream(resContent).toString().trim();
} else {
}
listItems.removeAll(listItems);
JSONArray ja = new JSONArray(line);
for (int i = 0; i < ja.length(); i++) {
p = new Post();
JSONObject jo = (JSONObject) ja.get(i);
p.setStory(jo.getString("story"));
p.setFavorite(false);
p.setTime(jo.getString("time"));
p.setPid(Integer.parseInt(jo.getString("pid")));
p.setUserName(jo.getString("username"));
p.setImagePath(jo.getString("profilepicture"));
listItems.add(p);
}
} catch (Exception e) {
progress.dismiss();
}
return listItems;
}
}
public类OutKrys扩展了ListActivity{
私人进展对话进展;
列表视图列表视图1;
private ArrayList listItems=new ArrayList();
专用PostAdapter post_适配器;
公共静态DefaultHttpClient httpclient=新的DefaultHttpClient();
私有类PostAdapter扩展了ArrayAdapter{
private ArrayList items=new ArrayList();
公共PostAdapter(上下文,int textViewResourceId,
ArrayList项目){
super(上下文、textViewResourceId、项);
这个项目=项目;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
试一试{
视图v=转换视图;
如果(v==null){
LayoutInflater vi=(LayoutInflater)getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);
v=vi.充气(R.布局.输出,空);
}
Post o=项目。获取(位置);
如果(o!=null){
TextView tt=(TextView)v.findviewbyd(R.id.Story);
TextView bt=(TextView)v.findViewById(R.id.userName);
TextView tm=(TextView)v.findViewById(R.id.Time);
ImageView iv=(ImageView)v.findViewById(R.id.OutKryImageView01);
如果(tt!=null){
tt.setText(o.getStory());
}
如果(bt!=null){
bt.setText(o.getUserName());
}
如果(tm!=null){
tm.setText(o.getTime());
如果(iv!=null)
{
Drawable Drawable=LoadImageFromWebOperations(“abc.com/”+o.getImagePath());
iv.设置图像可绘制(可绘制);
}
}
}
返回v;
}捕获(例外e){
进步。解散();
返回null;
}
}
}
私有可绘制LoadImageFromWebOperations(字符串url)
{
尝试
{
InputStream为=(InputStream)新URL(URL).getContent();
Drawable d=Drawable.createFromStream(是“src名称”);
返回d;
}捕获(例外e){
System.out.println(“Exc=“+e”);
返回null;
}
}
@凌驾
受保护的void onListItemClick(列表视图l、视图v、整数位置、长id){
super.onListItemClick(左、右、位置、id);
//获取已单击的项目
对象o=this.getListAdapter().getItem(位置);
字符串关键字=o.toString();
Toast.makeText(您选择的“+”位置,Toast.LENGTH\u LONG)
.show();
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//setContentView(R.layout.listitems);
//listView1=(ListView)findViewById(R.id.ListView01);
试一试{
progress=ProgressDialog.show(OutKrys.this,“请稍候…”,
“装货清单”,正确);
线程t=新线程(){
公开募捐{
listItems=getList();
芬德勒邮报(mUpdateResults);
}
};
t、 start();
}捕获(例外e){
//进步。解散();
Toast.makeText(OutKrys.this,“dsadsa”+e,Toast.LENGTH_LONG.show();
}
}
final Handler pHandler=new Handler();
//创建可运行的过帐
最终可运行mUpdateResults=新可运行(){
公开募捐{
updateTabData();
}
};
私有void updateTabData(){
试一试{
this.post_Adapter=new PostAdapter(选中此,android.R.layout.simple_list_项,
清单项目);
this.setListAdapter(新的PostAdapter)(this,R.layout.outkrys,
列表项目);;
进步。解散();
}捕获(例外e){
//进步。解散();
}
}
私有ArrayList getList(){
试一试{
字符串loginURI=“abcd.com”;
HttpPost HttpPost=新的HttpPost(loginURI);
List nvps=new ArrayList();
添加(新的BasicNameValuePair(“authenticate”、“authenticate”);
添加(新的BasicNameValuePair(“选项卡”、“最近”);
setEntity(新的UrlEncodedFormEntity(nvps,HTTP.UTF_8));
HttpResponse响应=null;
response=KryAbout.httpclient.execute(httpost);
HttpEntity=response.getEntity();
职位p=新职位();
字符串行=”;
如果(实体!=null){
InputStream resContent=response.getEntity().getContent();
line=KryAbout.ReadInputStream(resContent.toString().trim();
}否则{
}
listItems.removeAll(listItems);
JSONArray ja=新JSONArray(线);
对于(int i=0;i
我认为您应该使用
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
.....
}
而不是
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id)
{
.....
}
@覆盖
公共视图单击(适配器视图a、视图v、内部位置、长id)
{
.....
}
我认为您应该使用
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
.....
}
而不是
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id)
{
.....
}
@覆盖
公共视图单击(适配器视图a、视图v、内部位置、长id)
{
.....
}
hi,我将代码更改为onListItemClick,但它仍然不起作用。setListAdapter(新的PostAdapter(this,R.layout.outkrys,listItems));这在线程中工作嗨,我将代码更改为onListItemClick,但仍然不工作。此.setListAdapter(新的PostAdapter(第