Listview,onItemClickListener和EditText

时间:2012-04-03 19:17:21

标签: android listview android-edittext onitemclicklistener

我有一个带有EditText的ListView。 实际上,当我触摸Listview的元素时,EditText有焦点并且键盘出现。好。 问题是我想通过listView的onItemClickListener在这个EditText上做一些事情,但似乎我的代码永远不会进入这个方法。

我在列表视图中尝试了一些setDescendantFocusability,但没有解决问题。

非常感谢。

public class NoteAdapter extends BaseAdapter {

private ArrayList<String> notes;
private LayoutInflater inflater;
private Context context;

public NoteAdapter(Context context, ArrayList<String> notes) {
    inflater = LayoutInflater.from(context);
    this.notes = notes;
    this.context = context;
}

public int getCount() {
    // TODO Auto-generated method stub
    return notes.size();
}

public Object getItem(int position) {
    // TODO Auto-generated method stub
    return notes.get(position);
}

public long getItemId(int id) {
    // TODO Auto-generated method stub
    return id;
}


private class ViewHolder {
    EditText note;
}

public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    ViewHolder holder;
    if(convertView == null) {
        holder = new ViewHolder();
        convertView = inflater.inflate(R.layout.listenote, null);
        holder.note = (EditText)convertView.findViewById(R.id.note);


        convertView.setTag(holder);
    }else {
        holder= (ViewHolder) convertView.getTag();
    }

    holder.note.setText(notes.get(position));

    return convertView;


}


}

我的主要活动

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    notes = new ArrayList<String>();
    for(int i=0; i< 10; i++)
        notes.add("note"+i);
    EditTextSelected = null;

    adapter = new NoteAdapter(this, notes);
    lv1 = ((ListView)findViewById(R.id.listeNote));
    lv1.setAdapter(adapter);
    lv1.setClickable(true);

    lv1.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> a, View v, int position, long id) {
            Toast t = Toast.makeText(FastItActivity.this, "hello", 200);
            t.show();

        }
    });

listenote.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
android:id="@+id/widget1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">


    <EditText
    android:id="@+id/note"
    android:textColor="@color/black"
    android:textSize="12dp"
    android:layout_width="wrap_content"
    android:layout_height="40dp"
    android:padding="5dp"
    android:inputType="textMultiLine"
    android:scrollHorizontally="false"
    android:gravity="top|left"
    android:ems="10"
    android:layout_margin="10dp" 
    android:background="@drawable/fond_note"
    />



 </TableLayout>

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
android:id="@+id/widget1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/wooden_top"

>

<ListView 
    android:id="@+id/listeNote"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:background="@android:color/transparent"
    android:cacheColorHint="#00000000"
    android:isScrollContainer="false"
    android:divider="#00000000"

    >
</ListView>


</TableLayout>

4 个答案:

答案 0 :(得分:2)

将您的修改从ListView的{​​{1}}移至onItemClickListener的{​​{1}}

EditText的{​​{1}}:

onClickListener

或试试这个:

NoteAdapter

getView方法有位置参数,因此您可以区分点击holder.note = (EditText)convertView.findViewById(R.id.note); holder.note.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //do something } }); 的内容(如果您需要使用不同holder.note.setOnFocusListener(new View.OnFocusListener(){ @Override public void onFocus(){ //do something } } 的不同操作)< / p>

答案 1 :(得分:0)

这篇文章很长,但在中间/结尾,他演示了一个交互式列表视图,这正是您所需要的。 http://www.vogella.de/articles/AndroidListView/article.html

如果您可以发布一些有用的代码。

答案 2 :(得分:0)

我试图解决类似的问题:当您在列表项中嵌入多个视图时,列表中的哪个项目被选中了?!

我拒绝为列表中的每个项目创建一个新的侦听器。我无法想象在这样一个资源有限的平台上可以很好地扩展。但是,我发现您可以通过专门设置EditText来设置和检索onClick中的所选索引来解决此问题。

定义您的专业化:

package userInterface;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.EditText;

public class IndexedEditText extends EditText {
    public int listIndex;

    public IndexedEditText(Context context) {
        super(context);
    }

    public IndexedEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public IndexedEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
}

在列表 item XML声明中替换您的专业类的EditText。一定要正确地获取新类的路径(在我的例子中是userInterface.IndexedEditText)。

<?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" >

    <userInterface.IndexedEditText
        android:id="@+id/et_first_item"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:hint="@string/select"
        android:inputType="none" />

    <!-- other views -->


</RelativeLayout>

listIndex中设置getView,并为每个OnClickListener实例设置IndexedEditText

public abstract class EditTextPairArrayAdapter <T> extends ArrayAdapter<T> {

    LayoutInflater inflater;

    static class ViewHolder {
        private WeakReference<IndexedEditText>  name;
        private WeakReference<EditText>  notes;

        public ViewHolder(IndexedEditText tv, EditText et) {
            name = new WeakReference<IndexedEditText>(tv);
            notes = new WeakReference<EditText>(et);
        }
    }


    int textViewId;
    int editTextId;
    int listItemId;
    List<T> list = null;
    WeakReference<Context> contextRef;


    //context is Activity that instantiates this array adapter
    //resourceId is the layout xml ID for your special row
    //textViewResourceId is any TextView ID in your special row xml def
    //editTextResourceId means nothing in this context
    //objects is the initial list of objects to present in UI
    public EditTextPairArrayAdapter(Context context, int resourceId, int textViewResourceId, int editTextResourceId, List<T> objects)  {
        super(context, resourceId, textViewResourceId, objects);

        this.listItemId = resourceId;
        this.textViewId = textViewResourceId;
        this.editTextId= editTextResourceId;
        this.list = objects;
        this.contextRef = new WeakReference<Context>(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        View       view       = null;
        ViewHolder viewHolder = null;

        if (convertView == null) {
            if(inflater == null)
                inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(listItemId, null);

            IndexedEditText text = (IndexedEditText)view.findViewById(textViewId);
            EditText notes = (EditText) view.findViewById(editTextId);

            text.listIndex = position;
                    //Special sauce

            if(contextRef != null && contextRef.get() != null && (contextRef.get() instanceof View.OnClickListener)) {
                text.setOnClickListener((View.OnClickListener) contextRef.get());
            }

            viewHolder = new ViewHolder(text, notes);

            view.setTag(viewHolder);
        }
        else {
            view = convertView;
            viewHolder = (ViewHolder) convertView.getTag();
        }


        Titem = this.getItem(position);
        if(item != null) {
            //special sauce
        }

        return view;
    }

    //add abstract methods for implementations to define special sauce
}

最后,在实现OnClickListener的Activity中:

public void onClick(View v) {
    if(v instanceof IndexedEditText) {
        Object obj = myList.get(((IndexedEditText)v).listIndex);
        //do stuff with obj         
    }
}

答案 3 :(得分:0)

我发现的解决方案是将 EditText 与 TextView 叠加,并在两者上交替消失/可见,以便 EditText 在隐藏时停止引发错误。