自定义列表视图显示空行

时间:2014-12-03 08:48:52

标签: android

我的自定义列表视图显示空行,但是当我使用logcat显示数据时,它显示数据全部存在于我的arraylist中...如何使listview显示我的arraylist项目?

public class Myarrayadapter extends ArrayAdapter<String> {

Context context;
ArrayList<String> values;
int resid;

public Myarrayadapter(Context context, int resid, ArrayList<String> values) {
    super(context, resid, values);
    this.resid = resid;
    this.context = context;
    this.values = values;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(resid, parent, false);
    return rowView;
}
}

CLASSS HERE //////////////////////////////////////////// < / p>

    final ListView HomeListView = (ListView) findViewById(R.id.HomeListView);
    String[] filelist = fileList();
    ArrayList<String> fileList = new ArrayList<String>();

    for (int i=0; i < filelist.length; i++) {//displays only first half of job number
          String[] stemp = filelist[i].split("\\.");
        if(!fileList.contains(stemp[0])) {//makes sure only one job number is show in the list
            fileList.add(stemp[0]);
        }
    }
    Collections.reverse(fileList);
    Myarrayadapter listAdapter = new Myarrayadapter(this, R.layout.homelistview, fileList);
    HomeListView.setAdapter(listAdapter);
    HomeListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //get the clicked items job no. and pass it to the next activity which will display all relevant jobs
            Intent detailedlist_intent = new Intent(Home.this, Home2.class);
            detailedlist_intent.putExtra("Jobno", HomeListView.getItemAtPosition(position).toString());
            startActivity(detailedlist_intent);
        }
    });

2 个答案:

答案 0 :(得分:1)

用于创建自定义列表           1创建模型以保存每个列表视图行的数据。           2.将数据保存到模型。           3.在ArrayList中获取每个Model类对象。           4.创建自定义适配器。将Arraylist传递给适配器。适配器为每个listview行递归调用,并从ArrayList中的Model获取数据。           5.在Adapter中为每个Listview行调用xml文件(在Adapter中使用LayoutInflater)。           6.内部MainActivity将适配器设置为ListView           7.在AnadroidMainifest.xml文件中定义List

供参考,您可以使用以下链接

http://androidexample.com/How_To_Create_A_Custom_Listview_-_Android_Example/index.php?view=article_discription&aid=67&aaid=92

答案 1 :(得分:1)

您应该将值设置为渣油布局中的textview。您没有将文本设置为textview。这就是为什么它会变空。