我知道有很多关于如何制作 Android ListView 的帖子,但即使浏览了其中的大部分,我也无法弄清楚我的问题是什么。我是 Android 的新手,显然有点不知所措。 Activity 运行但不显示任何内容。
这是我的 Activity :
package com.example.myfirstapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
public class HistoryActivity extends Activity {
CustomRowAdapter customRowAdapter;
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
listView = (ListView)findViewById(R.id.listView);
customRowAdapter = new CustomRowAdapter(getApplicationContext());
listView.setAdapter(customRowAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.history, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是 activity_history.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical">
<ListView
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
ListView 中输入的 xml (custom_row.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical">
<TextView
android:id="@+id/textView1"
android:background="@drawable/box"
android:textSize="18sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/app_name" />
<TextView
android:id="@+id/textView2"
android:background="@drawable/box"
android:textSize="18sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/app_name" />
<TextView
android:id="@+id/textView3"
android:background="@drawable/box"
android:textSize="18sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/app_name" />
</LinearLayout>
和 CustomRowAdapter.java:
package com.example.myfirstapp;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class CustomRowAdapter extends ArrayAdapter<String> {
private String[] text1 = {
"Google Plus",
"Twitter",
"Facebook",
"Instagram"};
private String[] text2 = {
"test1",
"test2",
"test3",
"test4"};
private String[] text3 = {
"Google Plus",
"Twitter",
"Facebook",
"Instagram"};
private LayoutInflater layoutInflater;
private Context context;
public CustomRowAdapter(Context context) {
super(context,0);
layoutInflater= (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.context=context;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup){
view = layoutInflater.inflate(R.layout.custom_row,null);
TextView textViewT1 = (TextView)view.findViewById(R.id.textView1);
TextView textViewT2 = (TextView)view.findViewById(R.id.textView2);
TextView textViewT3 = (TextView)view.findViewById(R.id.textView3);
// textViewT1.setText("test");
// textViewT2.setText(text2[0]);
// textViewT3.setText(text3[0]);
return view;
}
@Override
public int getCount(){
return 0;
}
}
希望有人能帮帮我。
最佳答案
您的 ListView 没有显示任何内容是因为
@Override
public int getCount(){
return 0;
}
getCount
必须返回属于您的数据集的元素数。例如
@Override
public int getCount(){
return text1.length;
}
关于java - 带有 ArrayAdapter 的 Android ListView 不显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26551416/