我的第一个问题,如果出现问题就很抱歉。
我在显示自定义列表视图方面遇到问题,我尝试在此处执行类似操作:http://www.learn2crack.com/2013/10/android-custom-listview-images-text-example.html
首先,在MainActivity中我使用的是ViewFlipper,但我认为这并不重要。
vieFli = (ViewFlipper) findViewById(R.id.viewFlipper);
vieFli.setHorizontalScrollBarEnabled(true);
View vie2 = View.inflate(this, R.layout.activity_friends, null);
vieFli.addView(vie2);
这是activity_friends.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".FriendsActivity">
<Button
android:id="@+id/add_friend_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Add friend" />
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/add_friend_button"></ListView>
</RelativeLayout>
这是FriendList.java(只是适配器):
public class FriendList extends ArrayAdapter<String> {
private final Activity context;
private final String[] friendName;
private final Integer[] imageId;
public FriendList(Activity context, String[] friendName, Integer[] imageId) {
super(context, R.layout.one_row, friendName);
this.context = context;
this.friendName = friendName;
this.imageId = imageId;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.one_row, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
txtTitle.setText(friendName[position]);
imageView.setImageResource(imageId[position]);
//txtTitle.setVisibility(View.VISIBLE);
//imageView.setVisibility(View.VISIBLE);
return rowView;
}
@Override
public int getCount() {
return friendName.length;
}
@Override
public String getItem(int arg0) {
return friendName[arg0];
}
@Override
public long getItemId(int arg0) {
return arg0;
}
}
接下来我想告诉你的是one_row.xml(我想如何在ListView中表示一行):
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow>
<ImageView
android:id="@+id/img"
android:layout_width="50dp"
android:layout_height="50dp"/>
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="50dp" />
</TableRow>
</TableLayout>
在ViewFlipper中加载的FriendsActivity:
public class FriendsActivity extends ActionBarActivity {
ListView list;
String[] friendName = {
"Michael",
"James"
};
Integer[] imageId = {
R.drawable.image1,
R.drawable.image2
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_friends);
FriendList adapter = new FriendList(this, friendName, imageId);
list = (ListView) findViewById(R.id.list);
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// Toast.makeText(FriendsActivity.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
}
});
}
...
这只是我在帖子开头链接的教程的副本,它对我不起作用。你能告诉我问题出在哪里吗?当我读到其他一些问题时,我看到了xml布局的问题,但在我的内容中找不到它。
提前感谢您的帮助。
答案 0 :(得分:0)
尝试更改getView(...)
View rowView = inflater.inflate(R.layout.one_row, null, true);
是:
View rowView = inflater.inflate(R.layout.one_row, parent, false);