listview不会从其他类填充

时间:2017-06-26 13:06:02

标签: java android listview

我是java和android的新手(之前用C#编程)。 我正在尝试使用自定义布局填充列表视图,但没有任何反应 listview位于主要活动中,并且正在从单独的类中更新。 这里是我试图找到问题的所有东西:
    activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.nasir.pre_alpha.MainActivity"
    android:orientation="vertical">


    <Button
        android:text="Get Messages"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/msg_list"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="18dp"
        android:onClick="onClick" />

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="15dp"
        android:layout_below="@+id/msg_list"
        android:id="@+id/msg_listview" />
</RelativeLayout>

list_view_layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/linearlayout">

    <TextView
        android:text="Number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView" />

    <TextView
        android:text="NUMBER"
        android:layout_width="171dp"
        android:layout_height="wrap_content"
        android:id="@+id/txt_number" />

    <TextView
        android:text="Message Text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView3" />

    <TextView
        android:text="TEXT"
        android:layout_height="wrap_content"
        android:id="@+id/txt_msg"
        android:layout_width="171dp" />
</LinearLayout>

MainActivity.java:

package com.nasir.pre_alpha;
import android.Manifest;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import java.util.ArrayList;


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActivityCompat.requestPermissions(this ,new String[]{Manifest.permission.READ_SMS},255);



    }
    public void onClick (View l){
        ContentResolver rslv=getContentResolver();
        final String[] p=new String[]{"*"};
        Uri uri=Uri.parse("content://sms");
        Cursor cursor=rslv.query(uri,p,null,null,null);
        cursor.moveToFirst();
        ArrayList<String> numbers=new ArrayList<>();
        ArrayList<String> msg_body=new ArrayList<>();
        while (cursor.moveToNext()){
            numbers.add(cursor.getString(cursor.getColumnIndex("address")));
            msg_body.add(cursor.getString(cursor.getColumnIndex("body")));

        }
        model m=new model();
        view v=new view();
        controller c=new controller(m,v,getApplicationContext());
        c.set_data(numbers,msg_body);
        c.view_data();
    }

}

model.java:

package com.nasir.pre_alpha;

import java.util.ArrayList;

/**
 * Created by Rhasta on 6/26/2017.
 */

public class model {
   private ArrayList<String> numbers=new ArrayList<>();
    private ArrayList<String> msg_body=new ArrayList<>();
   public ArrayList<String> get_numbers(){
        return numbers;
    }
    public ArrayList<String> getMsg_body(){
        return msg_body;
    }
    public void set_numbers(ArrayList<String> numbers){
        this.numbers=numbers;
    }
    public void set_Msgbody(ArrayList<String> msg_body){
        this.msg_body=msg_body;
    }
}

view.java:

package com.nasir.pre_alpha;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

/**
 * Created by Rhasta on 6/26/2017.
 */

public class view {
    private Context c;

    private class viewadapter extends BaseAdapter{
        private   ArrayList<String> numbers=new ArrayList<>();
        private   ArrayList<String> msg_body=new ArrayList<>();
        public viewadapter(ArrayList<String> numbers,ArrayList<String> msg_body){
            this.numbers=numbers;
            this.msg_body=msg_body;
        }
        public int getCount(){
            return numbers.size();
        }
        public View getView(int pos, View view, ViewGroup viewGroup){
            LayoutInflater inflater=(LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View row=inflater.inflate(R.layout.list_view_layout,null);
            TextView number=(TextView)row.findViewById(R.id.txt_number);
            number.setText(numbers.get(pos));
            TextView msg_body=(TextView)row.findViewById(R.id.txt_msg);
            Toast.makeText(c,msg_body.getText(),Toast.LENGTH_SHORT).show();
            msg_body.setText(this.msg_body.get(pos));
            return row;
        }
        public long getItemId(int pos){
            return pos;
        }
        public Object getItem(int arg){
            return null;
        }
    }
    void set_context(Context context){
        c=context;
    }
    public void update_data(ArrayList<String> numbers,ArrayList<String> msg_body){
        viewadapter adapter=new viewadapter(numbers,msg_body);
        LayoutInflater inflater=(LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v=inflater.inflate(R.layout.activity_main,null);            
        ListView lst=(ListView)v.findViewById(R.id.msg_listview);
        lst.setAdapter(adapter);

    }
}

controller.java:

package com.nasir.pre_alpha;

import android.content.Context;

import java.util.ArrayList;

/**
 * Created by Rhasta on 6/26/2017.
 */

public class controller {
    private model m;
    private view v;
    private Context c;
    public controller(model m,view v,Context c){
        this.m=m;
        this.v=v;
        this.c=c;
    }
    public void set_data(ArrayList<String> numbers,ArrayList<String> msg_body){
        this.m.set_numbers(numbers);
        this.m.set_Msgbody(msg_body);
    }
    public void view_data(){
        v.set_context(c);
        v.update_data(m.get_numbers(),m.getMsg_body());
    }
}

但我找不到问题。 列表视图只是空的(不会显示)

2 个答案:

答案 0 :(得分:0)

您没有初始化列表视图,但您还没有为其分配项目列表。尝试:

ListView listview = (ListView) findViewById(R.id.msg_listview);

listview.setAdapter(.......);

答案 1 :(得分:0)

我真的无法理解你的代码。我建议你这样做:

添加:

<uses-permission android:name="android.permission.READ_CONTACTS" />
在你的清单中

而不是:

ActivityCompat.requestPermissions(this ,new String[]{Manifest.permission.READ_SMS},255);

<强> MainActivity

public class MainActivity extends AppCompatActivity {

    private   ArrayList<model> list = new ArrayList<>();

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

    viewadapter adapter = new viewadapter(list, getApplicationContext());
    ListView lst = (ListView) findViewById(R.id.msg_listview);
    lst.setAdapter(adapter);

}

public void onClick (View l){
    ContentResolver rslv = getContentResolver();

    final String[] p = new String[]{"*"};
    Uri uri = Uri.parse("content://sms");

    Cursor cursor = rslv.query(uri, p, null, null, null);
    cursor.moveToFirst();

    while (cursor.moveToNext()){
        String address = cursor.getString(cursor.getColumnIndex("address"));
        String body = cursor.getString(cursor.getColumnIndex("body"));

        model m = new model();
        m.setNumbers(address);
        m.setMsgbody(body);

        list.add(m);

    }

}

}

<强>模型

public class model {

   String numbers;
   String msg_body;

   public String getNumbers(){
     return numbers;
   }

   public String getMsg_body(){
     return msg_body;
   }

   public void setNumbers(String numbers){
     this.numbers = numbers;
   }

   public void setMsgbody(String msg_body){
     this.msg_body = msg_body;
   }
}

<强> viewadapter

private class viewadapter extends BaseAdapter{

    private   ArrayList<model> list = new ArrayList<>();
    private Context context;

    public viewadapter(ArrayList<model> list, Context context){
        this.list = list;
        this.context = context;
    }

    public int getCount(){
        return list.size();
    }

    public View getView(int pos, View view, ViewGroup viewGroup){

        View row = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_view_layout, null);

        model model = list.get(pos)
        TextView number = (TextView) row.findViewById(R.id.txt_number);
        number.setText(model.getNumbers);

        TextView msg_body = (TextView) row.findViewById(R.id.txt_msg);
        Toast.makeText(c, msg_body.getText(), Toast.LENGTH_SHORT).show();
        msg_body.setText(model.getMsg_body());

        return row;
    }

    public long getItemId(int pos){
        return pos;
    }
    public Object getItem(int arg){
        return list.get(arg);
    }
}

}

我认为不需要控制器类