2013-12-16 46 views
0

我的應用程序中有一個片段,它從parse.com獲取數據並將其顯示在ListView中。在Android ListView中單獨顯示的TextViews

我從Parse中檢索兩個字符串值。我的問題是,兩個字符串值被顯示在在ListView單獨行(見下圖)

http://postimg.org/image/vsaw8lsbh/33225549/

我的目標是具有相同的行作爲對象中的時間。

listview_main.xml

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

<ListView 
    android:id="@+id/listview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"/> 

listview_item.xml

<?xml version="1.0" encoding="utf-8"?> 

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/text" 
android:layout_width="fill_parent" 
android:layout_height="100dp" 
android:padding="5sp" 
android:textSize="15sp" /> 

fragmentClass

public class TuesdayFragment extends Fragment { 



// Declare Variables 
ListView listview; 
List<ParseObject> ob; 
ProgressDialog mProgressDialog; 
ArrayAdapter<String> adapter; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    // inflat and return the layout 
    View v = inflater.inflate(R.layout.listview_main, container, false); 
    Parse.initialize(getActivity(), "***parsekey***", "***parsekey***"); 


    new RemoteDataTask().execute(); 


    return v; 
} 

private class RemoteDataTask extends AsyncTask<Void, Void, Void> { 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     // Create a progressdialog 
     mProgressDialog = new ProgressDialog(getActivity()); 
     // Set progressdialog title 
     mProgressDialog.setTitle("Loading Programme, Please Wait :)"); 
     // Set progressdialog message 
     mProgressDialog.setIndeterminate(false); 
     // Show progressdialog 
     mProgressDialog.show(); 

    } 



    @Override 
    protected Void doInBackground(Void... params) { 

      try { 
       // Locate the class table named "Country" in Parse.com 
       ParseQuery<ParseObject> query = new ParseQuery<ParseObject> ("Program"); 
       query.orderByAscending("pSession"); 
       ob = query.find(); 

      } catch (ParseException e) { 
       Log.e("Error", e.getMessage()); 
       e.printStackTrace(); 
      } 



     return null; 
    } 

protected void onPostExecute(Void result){ 



    listview = (ListView) getView().findViewById(R.id.listview);   
    adapter = new ArrayAdapter<String>(getActivity(), R.layout.listview_item); 
    for (ParseObject country : ob) { 
     adapter.add((String) country.get("pSubject")); 
     adapter.add((String) country.get("pTime")); 
    }  
    listview.setAdapter(adapter);  
    mProgressDialog.dismiss(); 
    } 

} 
} 
+0

你需要一些比ArrayAdapter 更復雜的東西。 SimpleAdapter可能是一個簡單的方法。 – njzk2

回答

1

變化

adapter.add((String) country.get("pSubject")); 
adapter.add((String) country.get("pTime")); 

adapter.add((String) country.get("pSubject") + "\n" + (String) country.get("pTime")); 
+0

工作就像一個魅力:D可能在這裏推它一下,但我可以控制他們的位置編程? –

+0

你也可以在你的代碼中解釋「\ n」嗎? –

+0

'\ n'是新行的控制字符。 http://en.wikipedia.org/wiki/Control_character – kord

0

的問題是您的適配器只接受一個TextView的(列表視圖項只有一個TextView的)。

每次你做adapter.add,你要添加一個新行到你的列表中。

更改listview_item,以便它有2個值:

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

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="5dp" 
     android:text="Large Text" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="5dp" 
     android:text="Medium Text" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

</LinearLayout> 

,那麼你必須添加自定義適配器,並填寫textviews INB的overrided方法getView,像這樣:

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

     View v = convertView; 

     if (v == null) { 

      LayoutInflater vi; 
      vi = LayoutInflater.from(getContext()); 
      v = vi.inflate(R.layout.listview_item, null); 

     } 

     final Country p = items.get(position); 

     if (p != null) { 

      TextView tt = (TextView) v 
        .findViewById(R.id.text);// nome 

      TextView date = (TextView) v 
        .findViewById(R.id.txt_date); 

      if (tt != null) { 
       tt.setText(country.get("pSubject")); 
      } 
      if (date != null) { 

       data.setText(country.get("pTime")); 
      } 
     return v; 

    } 

這裏有一個可以幫助你的教程:Custom listview Item row

+0

嗨,感謝您的回覆。在列表view_item中發生錯誤,說「錯誤的方向?沒有指定方向,默認是水平的,但這個佈局有多個孩子,其中至少有一個佈局寬=」match_parent「 –

+0

我編輯我的答案,以便它的佈局我使用的listview item單元格沒有問題,這個佈局文本並排排列,如果你希望它在下一行,改變佈局方向爲vertical:'android:orientation =「vertical」' – Dyna