我的ListView数据未显示

时间:2017-12-15 11:24:37

标签: android listview

我正在努力Listview,如何在此Listview中添加两个值首先是大的,第二个是小的,我使用的是CustomAdapter但是我无法添加两个值, Listview shows空值,如何添加两个值,我从不在我的适配器中放入mdata,请帮我看看如何显示listview?

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

ArrayList<Data> mData;
ArrayAdapter<CustomAdapter> mCustomAdapter;
ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listView= (ListView) findViewById(R.id.listView);
    mData= new ArrayList<Data>();

    mData.add(new Data("g1", "its g1"));
    mData.add(new Data("g2", "its g2"));
    mData.add(new Data("g3", "its g3"));
    mData.add(new Data("g4", "its g4"));

    mCustomAdapter = new ArrayAdapter<CustomAdapter>(getApplicationContext(), R.layout.row);


    listView.setAdapter(mCustomAdapter);
  }
}

MyCustomAdapter.Java

    class CustomAdapter extends BaseAdapter {
    Context mContext;
    ArrayList<ContactsContract.Data> mDataVariable;


    public CustomAdapter(Context context, ArrayList<ContactsContract.Data> dataVariable) {
        this.mContext = context;
        this.mDataVariable = dataVariable;

    }



    @Override
    public int getCount() {
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        RecyclerView.ViewHolder holder;

        LayoutInflater inflator = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);


        convertView = inflator.inflate(R.layout.row, null);

        TextView bigText = (TextView) convertView
                .findViewById(R.id.big_text);
        TextView smallText = (TextView) convertView
                .findViewById(R.id.small_text);
        CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);

        return convertView;
    }
}

Data.java

    public class Data{
    String t1;
    String t2;
    Boolean isSelected = false;

    public Data(String t1,String t2) {
        this.t1 = t1;
        this.t2 = t2;
    }

    public String t1() {
        return t1;
    }
    public String t2() {
        return t2;
    }
    public Boolean getIsSelected() {
        return isSelected;
    }

}

row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/checkbox"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/checkbox">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/big_text"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/small_text"/>
    </LinearLayout>

</RelativeLayout>

MainActivity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.dslenglish.httpswww.myapplication.MainActivity">

    <ListView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:choiceMode="multipleChoice"
        android:focusable="false"/>

</LinearLayout>

5 个答案:

答案 0 :(得分:1)

使用此

@Override
public int getCount() {
    return mDataVariable.size();
}

而不是这个

@Override
public int getCount() {
    return 0;
}
  

编辑1:

您未在getView()方法

中的控件中设置任何数据

您必须在TextviewgetView()方法

中设置数据
@Override
public View getView(int position, View convertView, ViewGroup parent) {
      // set data here like below code
       bigText.setText(mDataVariable.contactDtoList.get(position).getbigvalue());
       smallText.setText(mDataVariable.contactDtoList.get(position).getsmallvalue());
       checkBox.setChecked(mDataVariable.contactDtoList.get(position).getIsSelected());
}
  

编辑2

My row.xml hight改为android:layout_height="wrap_content",如下面的代码

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/checkbox"/>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@+id/checkbox">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/big_text"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/small_text"/>
</LinearLayout>

编辑3

使用此

mCustomAdapter = new CustomAdapter(MainActivity.this,mData);  

而不是

mCustomAdapter = new ArrayAdapter<CustomAdapter>(getApplicationContext(), R.layout.row,R.id.big_text);

编辑4

@Override
    public View getView(int position, View convertView, ViewGroup parent) {

        RecyclerView.ViewHolder holder;

        LayoutInflater inflator = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);


        convertView = inflator.inflate(R.layout.row, null);

        TextView bigText = (TextView) convertView
                .findViewById(R.id.big_text);
        TextView smallText = (TextView) convertView
                .findViewById(R.id.small_text);
        CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);

         bigText.setText(mDataVariable.contactDtoList.get(position).getbigvalue());
   smallText.setText(mDataVariable.contactDtoList.get(position).getsmallvalue());
   checkBox.setChecked(mDataVariable.contactDtoList.get(position).getIsSelected());
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    if(b){
                        Toast.makeText(mContext, "chceked", Toast.LENGTH_SHORT).show();
                    }else {
                        Toast.makeText(mContext, "Unchecked", Toast.LENGTH_SHORT).show();
                    }
                }
            });


        return convertView;
    }

FYI

这里好 tutorial for listview

答案 1 :(得分:0)

@Override
public int getCount() {
    return mDataVariable.size();
}

您应该在getCount()中返回arraylist大小,如上所示。在listview中也使用match_parent作为宽度。 同时ArrayList<ContactsContract.Data> mDataVariable;将其更改为ArrayList<Data> mDataVariable;

public CustomAdapter(Context context, ArrayList<Data> dataVariable) {
this.mContext = context;
this.mDataVariable = dataVariable;

}

将适配器初始化为

mCustomAdapter = new ArrayAdapter<CustomAdapter>(getApplicationContext(), mData);

答案 2 :(得分:0)

更改此

mCustomAdapter = new ArrayAdapter<CustomAdapter>(getApplicationContext(), R.layout.row,R.id.big_text);

行到

mCustomAdapter = new CustomAdapter(getApplicationContext(),mData);

也在适配器类

    @Override
public int getCount() {
    return mDataVariable.size();
}

答案 3 :(得分:0)

将数据填入您的getview

@Override
    public View getView(int position, View convertView, ViewGroup parent) {

        RecyclerView.ViewHolder holder;

        LayoutInflater inflator = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);


        convertView = inflator.inflate(R.layout.row, null);

        TextView bigText = (TextView) convertView
                .findViewById(R.id.big_text);
        TextView smallText = (TextView) convertView
                .findViewById(R.id.small_text);
        CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);

        bigText.setText(mDataVariable.contactDtoList.get(position).getbigvalue());
        smallText.setText(mDataVariable.contactDtoList.get(position).getsmallvalue());
        checkBox.setChecked(mDataVariable.contactDtoList.get(position).isChecked());

        return convertView;
    }

答案 4 :(得分:0)

首先更改您的CustomAdapter构造函数,因为它不具有Data类的对象。但ContactsContract.Data不同。

在CustomAdapter中更改这些行

ArrayList<ContactsContract.Data> mDataVariable;

public CustomAdapter(Context context, ArrayList<ContactsContract.Data> dataVariable){}

ArrayList<Data> mDataVariable;
public CustomAdapter(Context context, ArrayList<Data> dataVariable) {}

并返回Arraylist大小以显示数据。因此,请按以下方法进行更改。

@Override
public int getCount() {
   return mData.size();
} 

@Override
public Data getItem(int position) {
   return  mData.get(position);
}

在MainActivity中更改此行

 mCustomAdapter = new ArrayAdapter<CustomAdapter>(getApplicationContext(), R.layout.row,R.id.big_text);

 mCustomAdapter = new CustomAdapter(context,mData);  

希望这会有帮助。

更新getView()中添加此代码以获取复选框值

checkBox.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
            if(checkBox.isChecked())
            {
                Toast.makeText(mContext,"checkbox checked", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(mContext,"checkbox Unchecked", Toast.LENGTH_SHORT).show();
            }
      }
 });