我在Listview的onListItemClick()方法中遇到了问题。我无法获取列表的位置。
这是我的代码,请帮助我:
coverletterselection.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/bgg" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<ImageView
android:layout_width="fill_parent"
android:background="@drawable/blackstrip"
android:layout_height="wrap_content" />
<ImageView
android:layout_width="fill_parent"
android:id="@+id/coverletterbgView"
android:background="@drawable/imgselectcoverltrresume"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="wrap_content"
android:layout_marginLeft="5sp"
android:background="@drawable/submission"
android:layout_height="wrap_content" />
<ImageButton
android:layout_width="wrap_content"
android:background="@drawable/iconaddplus"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="10sp"/>
</RelativeLayout>
</RelativeLayout>
Java文件
public class MainActivity extends ListActivity implements OnItemClickListener
{
int ht,wt;
ListView list;
DataHelperCoverLetter dbcl;
ArrayList<String> coverLetterTitle = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.coverletterselection);
dbcl = new DataHelperCoverLetter(this);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
ht = displaymetrics.heightPixels;
wt = displaymetrics.widthPixels;
bg = (ImageView)findViewById(R.id.coverletterbgView);
bg.setLayoutParams(new LinearLayout.LayoutParams(wt, ht/3));
coverLetterTitle = dbcl.fetchCoverLetter();
//get the listView from your layout and define the listener on it
list = (ListView)findViewById(R.id.list);
list.setOnItemClickListener(this);
addCoverLetterBtn = (ImageButton)findViewById(R.id.addCoverLetter);
addCoverLetterBtn.setOnClickListener(this);
setListAdapter(new CoverLetterAdaptor(this,ht,wt,coverLetterTitle));
}
@Override
public void onListItemClick(ListView parent, View view, int position, long id)
{
Log.i("OnClick", "position = "+position);
}
}
public class CoverLetterAdaptor extends BaseAdapter
{
private LayoutInflater mInflater;
ArrayList<String> coverLetterItems;
Context context;
int ht,wt;
public CoverLetterAdaptor(Context context,int ht,int wt,ArrayList<String> coverLetterItems)
{
this.context = context;
this.coverLetterItems = new ArrayList<String>();
this.coverLetterItems = coverLetterItems ;
this.ht = ht;
this.wt = wt;
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return coverLetterItems.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
convertView = mInflater.inflate(R.layout.coverletteradaptor, null);
holder = new ViewHolder();
holder.coverLetterTxt = (TextView) convertView.findViewById(R.id.coverLetterAdaptorTxt);
holder.bgImageCCAdaptor = (ImageView)convertView.findViewById(R.id.bgimageCoverLetter);
holder.bgImageCCAdaptor.setLayoutParams(new RelativeLayout.LayoutParams(wt, ht/7));
convertView.setTag(holder);
holder = (ViewHolder) convertView.getTag();
holder.coverLetterTxt.setText(coverLetterItems.get(position));
return convertView;
}
static class ViewHolder
{
TextView coverLetterTxt;
ImageView bgImageCCAdaptor;
}
}
Log.i("OnClick", "position = "+position);
的输出是什么? - Ozair Kafray