我正在尝试编写一个应用程序,它从数据库中获取项目并在 ListView 中填充行。点击行后我无法点击这些项目,并且 dpad 也不会转到任何行。我正在使用自定义适配器。
推文.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 方法
@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;
}
更新
问题已解决,我想通了:DI 必须在我的适配器类中的这两种方法中返回 true 而不是 false!
@Override
public boolean areAllItemsEnabled()
{
return true;
}
@Override
public boolean isEnabled(int arg0)
{
return true;
}