2014-05-22 22 views
0

我正在解析json數據並使用BaseAdapter在自定義ListView中顯示。我有在解析數據時沒有問題。也沒有任何問題設置數據到ModelClass.java(setters和getters類)只有問題是如何從ModelClass.java獲取數據。請幫忙。無法理解如何從setters和getters類中獲取值

這是我MainActivity.java

public class MainActivity extends ActionBarActivity { 

    // url to make request(to get the latest 20 feeds). 
    private static String url = "http;....."; 
    // JSON Node names 
    private static final String TAG_BODY = "body"; 
    private static final String TAG_CREATED_AT = "created_at"; 
    private static final String TAG_DATE_TIME = "date_time"; 
    private static final String TAG_DEPARTEMENT = "department"; 
    private static final String TAG_ID = "id"; 
    private static final String TAG_INCLUDE = "include"; 
    private static final String TAG_MEDI_TYPE = "mediaType"; 
    private static final String TAG_PRIORITY = "priority"; 
    private static final String TAG_TITLE = "title"; 
    private static final String TAG_UPDATED_AT = "updated_at"; 
    private static final String TAG_USER_ID = "user_id"; 
    ProgressDialog progressDialog; 
    ListView listViewFeeds; 
    // String mTitle, mBody; 
    List<ModelClass> model = new ArrayList<ModelClass>(); 

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

     /* To display the feed */ 
     listViewFeeds = (ListView) findViewById(R.id.listViewFeeds); 
     listViewFeeds.setAdapter(new NewAdapter(this)); 

     Feeds feeds = new Feeds(); 
     feeds.execute(); 
    } 

    public class Feeds extends AsyncTask<Void, Void, Void> { 

     @Override 
     protected void onPreExecute() { 
      progressDialog = new ProgressDialog(MainActivity.this); 
      progressDialog.setTitle("Loading ..."); 
      progressDialog.show(); 
      super.onPreExecute(); 
     } 

     @Override 
     protected Void doInBackground(Void... arg0) { 
      HttpClient httpClient = new DefaultHttpClient(); 
      HttpGet httpGet = new HttpGet(url); 
      httpGet.setHeader("Accept", "application/json"); 
      try { 
       HttpResponse httpResponse = httpClient.execute(httpGet); 
       StatusLine statusLine = httpResponse.getStatusLine(); 
       int statusCode = statusLine.getStatusCode(); 
       if (statusCode != 200) { 
        return null; 
       } 
       HttpEntity httpEntity = httpResponse.getEntity(); 
       InputStream inputStream = httpEntity.getContent(); 
       BufferedReader bufferedReader = new BufferedReader(
         new InputStreamReader(inputStream)); 
       StringBuilder stringBuilder = new StringBuilder(); 
       String line; 
       while ((line = bufferedReader.readLine()) != null) { 
        stringBuilder.append(line); 
       } 
       String jsonData = stringBuilder.toString(); 

       // Displaying json data in logcat. 
       Log.d("Latest 20 feeds", jsonData); 

       JSONArray array = new JSONArray(jsonData); 
       for (int i = 0; i < array.length(); i++) { 
        JSONObject object = array.getJSONObject(i); 
        String mTitle = object.getString("title"); 
        String mBody = object.getString("body"); 
        System.out.println("Titlte: " + mTitle); 
        System.out.println("Body: " + mBody); 
        // Setting the data to ModelClass 
        ModelClass mc = new ModelClass(mTitle, mBody); 
       } 

      } catch (ClientProtocolException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      progressDialog.dismiss(); 
      super.onPostExecute(result); 
     } 

    } 

} 

class NewAdapter extends BaseAdapter { 
    ArrayList<ModelClass> list; 
    Context context; 

    NewAdapter(Context c) { 
     context = c; 
    } 

    @Override 
    public int getCount() { 
     return 0; 
     // return list.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return list.get(position); 
    } 

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

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

     LayoutInflater inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View row = inflater.inflate(R.layout.single_row_feed_view, parent, 
       false); 
     TextView title = (TextView) row.findViewById(R.id.textViewTitleFeeds); 
     TextView body = (TextView) row.findViewById(R.id.textViewBody); 

     ModelClass temp = list.get(position); 
     title.setText(temp.title); 
     body.setText(temp.body); 
     return row; 
    } 

} 

這是我ModelClass.java

public class ModelClass { 
    String title; 
    String body; 

    ModelClass(String title, String body) { 
    this.title = title; 
    this.body = body; 
    } 
} 

activity_main.xml中文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <ListView 
     android:id="@+id/listViewFeeds" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 
    </ListView> 

</LinearLayout> 

single_row_feed_view.xml文件(行出現的ListView )

<?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" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/textViewTitleFeeds" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@+id/viewColor" 
     android:ellipsize="end" 
     android:maxLines="2" 
     android:text="TextView" 
     android:textSize="12dp" /> 

    <TextView 
     android:id="@+id/textViewBody" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/textViewTitleFeeds" 
     android:layout_marginTop="3dp" 
     android:ellipsize="end" 
     android:maxLines="2" 
     android:text="TextView" 
     android:textSize="8dp" /> 

</RelativeLayout> 

回答

0

它有點難以制定出確切的問題,因爲你是問如何從getter和setter方法類的價值,但你ModelClass沒有getter方法或setter方法。

您可以將getter放入您的ModelClass中,但如果它在同一個包中,您可能在技術上不需要它們。

你應該看看這個教程。這對我幫助很大。 http://www.vogella.com/tutorials/AndroidListView/article.html

0

這是你要求的嗎?

//Inside your Model class: 

public String getTitle(){ 
    return this.title; 
    } 

    public String setTitle(String givenTitle){ 
    this.title = givenTitle; 
    } 

    public String getBody(){ 
    return this.body; 
    } 

    public String setTitle(String givenBody){ 
    this.body = givenBody; 
    } 
+0

三江源,但這不是我的問題。如何在列表中顯示? – Shiva

+0

如果你正在嘗試做我認爲你正在嘗試做的事,那麼你將不僅需要一個完整的Model類(你現在不是這個時候),而且還需要一個Adapter類。而且我知道這不是我的業務,​​但我認爲你應該在弄髒手之前閱讀它。你可以從這裏開始:http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/這是一個很好的教程,它幫助我實現你的要求。 – Selz

0

正如其他人所提到的,你需要在你的模型類中放置getter和setter。就將數值放入列表並格式化列表項而言,您需要一個自定義列表適配器。然後創建你想放入列表適配器的項目列表,然後顯示列表。下面是一個例子。

enter code here 
public class ExampleListAdapter extends ArrayAdapter<ExampleModel> { 

private Context      context; 
private ArrayList<ExampleModel> items; 

public ExampleListAdapter(Context context, ArrayList<ExampleModel> items) { 
    super(context, R.id.result_list_item_direction_icon, items); 

    this.context = context; 
    this.items = items; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    LayoutInflater inflater = null; 
    View rowView = convertView; 

    //Get rowView from inflater 
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    rowView = inflater.inflate(R.id.yourcustom_layout, parent, false); 

    // Get reference ids from the rowView 
    //example 
    TextView item = (TextView) rowView.findViewById(R.id.itemID); 


    // Set the values for items in custom list view 

    item.setText(items.get(position).yourGetter()); 


    return rowView; 
} 

}在你想使用它的類

然後...

enter code here 
    ArrayList<t> yourList = new ArrayList<t>(); 
    yourList = getValuesFromDBOROtherSource;//make sure the return value is list! 
    yourCustomAdapter = new ExampleListAdapter(getActivity(), yourList); 

    setListAdapter(yourCustomAdapter);