Android代码:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootView;
rootView = inflater.inflate(R.layout.view_contact_list, container, false);
listview = (ListView) rootView.findViewById(R.id.listView1);
Log.d("Contact List", "Before read method");
ReadContacts();
Log.d("Contact List", "After Read Method");
Log.d("Before ", "Before Adapter Initialization");
adapter = new ContactListAdapter(getActivity(), contact_list);
Log.d("After",
"After Adapter Initializaiton and before listview set adapter");
listview.setAdapter(adapter);
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
TextView name = (TextView) arg1
.findViewById(R.id.tvContactName);
TextView phone = (TextView) arg1
.findViewById(R.id.tvContactPhone);
Toast.makeText(
getActivity(),
"Name : " + name.getText().toString() + "Phone : "
+ phone.getText().toString(),
Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
适配器工作正常,我可以在列表视图中看到数据,但我无法点击列表项。没有回应
我设置了android:clickable =“true”
片段是视图寻呼机的一部分
我不能使用列表片段并想使用适配器..错误是什么?
我还在onActivityCreated()中粘贴了这个setonitemclick方法..它没有工作
列表视图的XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="false"
>
</ListView>
</LinearLayout>
我正在使用的适配器:
package com.example.eh;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
public class ContactListAdapter extends BaseAdapter
{
Context context;
List<Tuple> contact_list=new ArrayList<Tuple>();
public ContactListAdapter(Context ctx, List<Tuple> data)
{
context = ctx;
contact_list = data;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
//return contacts.size();
return contact_list.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return contact_list.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
public String getName(int arg0)
{
return contact_list.get(arg0).getContactName();
}
public String getPhone(int arg0)
{
return contact_list.get(arg0).getContactPhone();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder = null;
if (convertView == null) {
view = ((Activity) context).getLayoutInflater().inflate(R.layout.row_full_contact_list, parent, false);
holder = new ViewHolder(view);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.name.setText(getName(position));
holder.phone.setText(getPhone(position));
return view;
}
private class ViewHolder {
public TextView name;
public TextView phone;
public Button invite;
public ViewHolder(View v)
{
name = (TextView) v.findViewById(R.id.tvContactName);
phone = (TextView) v.findViewById(R.id.tvContactPhone);
invite = (Button) v.findViewById(R.id.bInviteFriends);
}
}
}
行项的XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/LeftSide"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/tvContactName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="30dp"
android:maxLines="1"
/>
<TextView
android:id="@+id/tvContactPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tvContactName"
android:text="TextView" />
</RelativeLayout>
<Button
android:id="@+id/bInviteFriends"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Invite" />
</RelativeLayout>
答案 0 :(得分:0)
您是否在breakpoint
方法中添加onItemClick
以确保其未被解雇?
尝试在listview's
XML上添加以下代码:
android:focusable="false"
答案 1 :(得分:0)
试试这个
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(
getActivity(),position.toString+" is the position you clicked!!",Toast.LENGTH_SHORT).show();
TextView name = (TextView) arg1
.findViewById(R.id.tvContactName);
TextView phone = (TextView) arg1
.findViewById(R.id.tvContactPhone);
Toast.makeText(
getActivity(),
"Name : " + name.getText().toString() + "Phone : "
+ phone.getText().toString(),
Toast.LENGTH_SHORT).show();
}
});