我知道有很多关于如何制作Android ListView的文章,但是即使仔细阅读了大多数文章,我仍然无法弄清楚我的问题是什么。我对Android完全陌生,显然有点不知所措。该活动将运行,但不显示任何内容。
这是我的活动:
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(custom_row.xml)中输入的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;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句