我正在使用 android 中的自定义 listView。但我无法单击列表视图中的项目。
我的代码是
适配器代码(BankArrayListAdapter.java)
package com.example.customlist;
import java.util.List;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class BankArrayListAdapter extends ArrayAdapter<Bank>{
private int resource;
private LayoutInflater inflater;
private Context ctx;
public BankArrayListAdapter(Context context, int resourceId, List<com.example.customlist.Bank> ls)
{
super(context, resourceId, ls);
resource = resourceId;
inflater = LayoutInflater.from( context );
ctx=context;
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
/* create a new view of my layout and inflate it in the row */
convertView = inflater.inflate( resource, null );
/* Extract the city's object to show */
Bank bank = (Bank) getItem(position);
/* Take the TextView from layout and set the city's name */
TextView txtName = (TextView) convertView.findViewById(R.id.textView1);
txtName.setText(bank.getName());
/* Take the TextView from layout and set the city's wiki link */
TextView txtWiki = (TextView) convertView.findViewById(R.id.textView2);
txtWiki.setTextSize(13);
txtWiki.setMovementMethod(LinkMovementMethod.getInstance());
txtWiki.setText(bank.getUrl());
/* Take the ImageView from layout and set the city's image */
ImageView imageCity = (ImageView) convertView.findViewById(R.id.imageView1);
String uri = "drawable/" + bank.getLogo();
int imageResource = ctx.getResources().getIdentifier(uri, null, ctx.getPackageName());
Drawable image = ctx.getResources().getDrawable(imageResource);
imageCity.setImageDrawable(image);
return convertView;
}
}
MainActivity.java
package com.example.customlist;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class Main extends Activity {
ListView lv;
List<Bank> ls;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv= (ListView) findViewById(R.id.listView1);
ls= new ArrayList<Bank>();
ls.add(new Bank("sbi","State Bank Of India","http://www.sbi.com"));
ls.add(new Bank("iob", "India Overseas Bank","http://www.iob.com"));
ls.add(new Bank("icici","ICICI","http://www.icici.com"));
//lv.setAdapter( new BankArraylistA);
lv.setAdapter( new BankArrayListAdapter(Main.this, R.layout.banklist, ls ) );
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Toast.makeText(Main.this,"Your Listener Works!",Toast.LENGTH_SHORT).show();
// System.out.println("Name: "+ls.get(position).getName());
// String s =(String) ((TextView) v.findViewById(R.id.From)).getText();
// Toast.makeText(Messages.this, s, Toast.LENGTH_LONG).show();
}
});
System.out.println(ls);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
activity_main.xml 文件是这样的
<LinearLayout 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:descendantFocusability="blocksDescendants"
tools:context=".Main" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</ListView>
</LinearLayout>
提前致谢
问候, 萨蒂什