2013-12-16 26 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