0
我的第一個問題在這裏,所以很抱歉,如果出現問題。Android自定義列表視圖 - 無法顯示
我有我的顯示列表視圖定製,我試圖做同樣喜歡這裏的問題: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加載:
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的佈局存在問題,但無法在我的系統中找到它。
感謝您的幫助。
仍然沒有幫助...我仍然只看到按鈕(也就是上面的列表) – Slawek 2015-04-04 20:57:30
問題是你的'ViewFliper',你應該檢查如何使用它 – Xcihnegn 2015-04-05 11:03:01