0

我无法在 ListView 上选择项目。我使用自定义适配器来设置 ListView 的内容。我已经设置了 ListView 的 OnItemClickListener。然而,它没有回应。我感谢你的帮助。这是我的代码:

列表项(connections.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="6dip" >


    <LinearLayout
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="0.90"
        android:orientation="vertical" >
        <TextView
        android:id="@+id/connname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"        
        android:text="@string/acc1" />
    <TextView
        android:id="@+id/conntype"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"    
        android:textSize="9sp"    
        android:text="@string/acctype1" />
    </LinearLayout>

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

包含 ListView (groupconnections.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="match_parent"
    android:orientation="vertical">
    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dip"  
        android:layout_marginBottom="5dip"
        android:orientation="vertical"
        android:id="@+id/textOperLayout">
        <TextView
            android:id="@+id/connectionsLabel"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/connections"
            android:textColor="@color/conn_text"
            android:textSize="25sp"
            android:paddingTop="5dp"
            android:paddingBottom="5dp"
            android:paddingLeft="15dp" 
            android:background="@color/conn_back"/>


        <EditText
            android:id="@+id/searchEditText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="@string/searchhint"
            android:layout_marginTop="10dp"
            android:inputType="textPersonName" />
    </LinearLayout>

    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dip"  
        android:layout_marginBottom="50dip"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_above="@+id/textOperLayout"
        android:id="@+id/listviewlayout">
        <ListView
            android:id="@+id/connectionlist"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:choiceMode="singleChoice"  />
    </LinearLayout>

    <Button
        android:id="@+id/addConnCommitButton"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="15dp"
        android:text="@string/commitToAdd" />

</RelativeLayout>

相关活动(AddMoreConnections.xml):

public class AddMoreConnections extends Activity implements OnItemClickListener{

    private ListView mainListView ;  
    private ArrayAdapter<AbstractHolder> listAdapter ; 
    private TextView searchConnTextView;
    private Button commitButton;
    private ArrayList<AbstractHolder> connlist;
    private ArrayList<AbstractHolder> listNotOnView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Generate list View from ArrayLis 

        setContentView(R.layout.groupconnections);  

        addListenerOnSearchConnTextView();
        //Initialize properties   
        mainListView = (ListView) findViewById( R.id.connectionlist );  
        mainListView.setOnItemClickListener(this);

        // Create and populate a List of planet names.
        listNotOnView = new ArrayList<AbstractHolder>();
        connlist = new ArrayList<AbstractHolder>(5);
        Iterator<AbstractHolder> iter = SocialRssModel.holders.values().iterator();
        while(iter.hasNext())
            connlist.add(iter.next());

        // Create ArrayAdapter using the planet list.  
        listAdapter = new CustomAdapter(this,
                R.layout.connlist, connlist);
        mainListView.setAdapter( listAdapter );

    }
    public void addListenerToFinishButton(){
        commitButton = (Button) findViewById(R.id.addConnCommitButton);
        commitButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {


            }
        });
    }
    public void addListenerOnSearchConnTextView(){
        searchConnTextView = (TextView) findViewById(R.id.searchEditText);
        searchConnTextView.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                int listNotOnViewsize = listNotOnView.size();
                int connlistsize = connlist.size();

                for(int i= 0; i < connlistsize; i++){
                    if(!connlist.get(i).connNameContains(s.toString())){
                        listNotOnView.add(connlist.remove(i));
                        i--;
                        connlistsize--;
                    }
                }
                for(int i=0; i < listNotOnViewsize; i++){
                    if(listNotOnView.get(i).connNameContains(s.toString())){
                        connlist.add(listNotOnView.remove(i));
                        i--;
                        listNotOnViewsize--;
                    }
                }
                ((CustomAdapter) listAdapter).updateList(connlist);
                listAdapter.notifyDataSetChanged();

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {                
            }

            @Override
            public void afterTextChanged(Editable s) {              
            }
        });
    }
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        AbstractHolder temp = (AbstractHolder)mainListView.getItemAtPosition(arg2);
        Intent i = new Intent(this, CategoryContentViewerController.class);
        Bundle b = new Bundle();
        b.putInt("AbstractHolderKey", temp.getId());
        i.putExtras(b);
        startActivity(i);
        finish();
    }
    private class CustomAdapter extends ArrayAdapter<AbstractHolder> {

          private ArrayList<AbstractHolder> connectionList;

          public CustomAdapter(Context context, int textViewResourceId, ArrayList<AbstractHolder> connList) {
              super(context, textViewResourceId, connList);
              this.connectionList = new ArrayList<AbstractHolder>();
              this.connectionList.addAll(connList);
          }
          public void updateList(ArrayList<AbstractHolder> connList){
              connectionList.clear();
              connectionList.addAll(connList);
          }
          private class ViewHolder {
              TextView name;
              TextView acctype;
              CheckBox sel;
          }



          @Override
          public View getView(int position, View convertView, ViewGroup parent) {

              ViewHolder holder = null;
              Log.v("ConvertView", String.valueOf(position));

              if (convertView == null) {
                  LayoutInflater vi = (LayoutInflater)(((Activity)this.getContext()).getSystemService(Context.LAYOUT_INFLATER_SERVICE));
                  convertView = vi.inflate(R.layout.connlist, null);

                  holder = new ViewHolder();
                  holder.name = (TextView) convertView.findViewById(R.id.connname);
                  holder.acctype = (TextView) convertView.findViewById(R.id.conntype);
                  holder.sel = (CheckBox) convertView.findViewById(R.id.checkBox1);
                  convertView.setTag(holder);


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

              AbstractHolder conn = connectionList.get(position);
              holder.name.setText(conn.getName());
              holder.acctype.setText(conn.getConntype());
              holder.sel.setChecked(conn.isSelected());
              holder.sel.setTag(conn);

              return convertView;

          }
    }
}
4

1 回答 1

0

它与connections.xml 中的复选框项目相关,即列表行项目。如果我删除复选框,相关的听众会响应。我认为,可能认为不需要setItemOnClickListener(..)方法,因为每一行都有一个复选框。或者这是一个错误。

于 2013-01-28T09:37:59.237 回答