2017-06-26 57 views
0

我是新來的java和android(在C#編程之前)。 我想填充一個自定義佈局的列表視圖,但沒有任何反應。
listview在主要活動中,並且正在從一個單獨的類中更新。 這裏是所有我試圖找到問題的東西:
activity_main.xml中listview不會從其他類填充

<?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()); 
    } 
} 

,但我不能發現問題。 listview只是空的(不會顯示)

+0

哪裏調用你的onClick方法? – Vij

+0

@Vij據我所知當事件觸發某個元素時(我將按鈕的onclick屬性設置爲在mainactivity.java文件中定義的onClick),在我的活動 –

回答

0

你沒有初始化列表視圖,你還沒有分配給它的項目列表。嘗試:

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

listview.setAdapter(.......); 
+0

中有一個按鈕。看看視圖。java –

+0

你沒有調用:update_data() –

+0

Update_data()被view_data()調用,我在MainActivity.java中調用了view_data() –

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); 
    } 
} 

} 

我不認爲有任何需要一個控制器類

+0

我試圖做到這一點的MVC方式。更新UI或抓取輸入的視圖類。模型類負責處理來自視圖的數據。控制器是這兩者之間的橋樑 –