无法使用自定义适配器单击ListView中的项目

时间:2013-03-12 02:54:22

标签: android listview

我正在尝试编写一个应用程序,它从数据库中获取项目并在ListView中填充行。点击行后我无法点击这些项目,而且dpad也不会转到任何行。我正在使用自定义适配器。

Tweet.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="?android:attr/listPreferredItemHeight"
        android:padding="6dip" 
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/textUser"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textStyle="bold" 
                android:text="NAME"/>

            <TextView
                android:id="@+id/textDate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#333333"
                android:text="DATE" />

        </LinearLayout>

        <TextView
            android:id="@+id/textTweet"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TWEET" />

    </LinearLayout>

activity_main.xml中

<?xml version="1.0" encoding="UTF-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    tools:context=".MainActivity" >

</ListView>

Oncreate Method

@Override
protected void onCreate(Bundle savedInstanceState)
{
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            ListView view = (ListView) findViewById(R.id.listView);     
        MyArrayAdapter theAdapter = new MyArrayAdapter(tweetDb,this);

        //setContentView(theAdapter.getView(0, null, null));
        view.setAdapter(theAdapter);
        view.setOnItemClickListener( new OnItemClickListener()
        {

            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position,
                    long id) 
            {
                //Tweet theTweet = (Tweet)parent.getAdapter().getItem(position);
                //saved.insert(theTweet);
                Toast.makeText(getApplicationContext(), "Saved", Toast.LENGTH_LONG).show();
                Log.v("SCHEMA", "onItemClick fired!");
            }
        } );
    }

适配器类getView

@Override
public View getView(int position, View convertView, ViewGroup parent) 
{
    LayoutInflater inflater = (LayoutInflater)context.getSystemService(
    Context.LAYOUT_INFLATER_SERVICE
    );

    View tweetView = inflater.inflate(R.layout.tweet, null);

    TextView textTweet = (TextView)  tweetView.findViewById(R.id.textTweet);    
    textTweet.setText(items.get(position).getTweet());

    textTweet = (TextView)tweetView.findViewById(R.id.textUser);
    textTweet.setText(items.get(position).getName());

    textTweet = (TextView)tweetView.findViewById(R.id.textDate);
    textTweet.setText(textFormat.format(items.get(position).getCreated()));

    return tweetView;
}

MyArrayAdapter类

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.content.Context;
import android.database.Cursor;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class MyArrayAdapter implements ListAdapter
{

    private ArrayList<Tweet> items;
    private Context context;
    private SimpleDateFormat textFormat = new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy");

    public MyArrayAdapter(TweetDbSource db, Context context)
    {
        items = new ArrayList<Tweet>();
        this.context = context;
        Cursor cursor = db.getReadableDatabase().query("Tweet", null, null, null, null, null, null);

        while (!cursor.isLast())
        {
                cursor.moveToNext();
                items.add(new Tweet(cursor.getLong(0),cursor.getLong(1),cursor.getString(2),cursor.getString(3),
                                cursor.getString(4),new Date(),cursor.getInt(6)));
        }
    }

    @Override
    public int getCount()
    {
        return items.size();
    }

    @Override
    public Object getItem(int position) 
    {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) 
    {
        return position;
    }

    @Override
    public int getItemViewType(int arg0) 
    {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(
        Context.LAYOUT_INFLATER_SERVICE
        );

        OnClickListener saveTweet = new OnClickListener() 
        {
              public void onClick(View v) 
              {
                  Toast.makeText(v.getContext(), "Saved", Toast.LENGTH_LONG).show();
              }
        };
        View tweetView = inflater.inflate(R.layout.tweet, null);

        TextView textTweet = (TextView)  tweetView.findViewById(R.id.textTweet);    
        textTweet.setText(items.get(position).getTweet());

        textTweet = (TextView)tweetView.findViewById(R.id.textUser);
        textTweet.setText(items.get(position).getName());

        textTweet = (TextView)tweetView.findViewById(R.id.textDate);
        textTweet.setText(textFormat.format(items.get(position).getCreated()));

        return tweetView;
    }


    @Override
    public int getViewTypeCount() 
    {
        // TODO Auto-generated method stub
        return 1;
    }

    @Override
    public boolean hasStableIds() 
    {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean isEmpty() 
    {
        // TODO Auto-generated method stub
        return items.size() == 0;
    }

    @Override
    public void registerDataSetObserver(DataSetObserver arg0) 
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver arg0) 
    {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean areAllItemsEnabled() 
    {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean isEnabled(int arg0) 
    {
        // TODO Auto-generated method stub
        return false;
    }

}

Toast或log无效,这就是我告诉它无法正常工作的方式

更新了getView方法

public View getView(int position, View convertView, ViewGroup parent) 
    {
             OnClickListener SaveView = new OnClickListener() {
             @SuppressLint("NewApi")
            public void onClick(View v) 
             {
               Toast.makeText(v.getContext(), "Saved", Toast.LENGTH_LONG).show();
               v.callOnClick();
             }
         };
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(
            Context.LAYOUT_INFLATER_SERVICE
            );
            View tweetView = inflater.inflate(R.layout.tweet, null);

            TextView textTweet = (TextView)  tweetView.findViewById(R.id.textTweet);    
            textTweet.setText(items.get(position).getTweet());

            textTweet = (TextView)tweetView.findViewById(R.id.textUser);
            textTweet.setText(items.get(position).getName());

            textTweet = (TextView)tweetView.findViewById(R.id.textDate);
            textTweet.setText(textFormat.format(items.get(position).getCreated()));
            tweetView.setOnClickListener(SaveView);
            return tweetView;
    }

更新

问题解决了,我想出来了:D我必须在我的适配器类中的这两个方法中返回true而不是false!

@Override
    public boolean areAllItemsEnabled() 
    {
        return true;
    }

    @Override
    public boolean isEnabled(int arg0) 
    {
        return true;
    }

5 个答案:

答案 0 :(得分:60)

通常会发生这种情况,因为列表视图中的项目是焦点。尝试添加

android:descendantFocusability="blocksDescendants"

在自定义列表视图行

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:padding="6dip" 
    android:orientation="vertical"
    android:descendantFocusability="blocksDescendants">

答案 1 :(得分:1)

我通常将点击侦听器放在适配器本身中:

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

        OnClickListener yourClickListener = new OnClickListener() {
              public void onClick(View v) {
                //put your desired action here
                v.callOnClick();
              }
          };

    ...

    // then add the listener to your view
    tweetView.setOnClickListener(yourClickListener);

答案 2 :(得分:0)

在onItemClick方法中添加:

v.setSelected(true);

答案 3 :(得分:-1)

将以下行添加到ListView xml

android:choiceMode="singleChoice"

答案 4 :(得分:-2)

试过这个?

        @Override
    public boolean isEnabled(int position) {
        //Set a Toast or Log over here to check.
        return true;
    }