TextViews在Android ListView中单独显示

时间:2013-12-16 15:40:35

标签: android listview parse-platform

我的应用程序中有一个片段,它从parse.com获取数据并将其显示在ListView中。

我正从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();  
    }

 }
 }

2 个答案:

答案 0 :(得分:1)

更改

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

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

答案 1 :(得分:0)

问题是您的适配器只接受一个textview(listview项目只有一个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>

然后你必须添加一个自定义适配器并在覆盖方法getView中填写textview,如下所示:

    @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