如何为Parse.com制作自定义ListView

时间:2014-09-09 14:04:21

标签: android listview parse-platform

我有一个列表视图,根据我的查询,在列表视图中显示餐馆的名称。但是我希望能够展示的不仅仅是每家餐厅的名称。我希望能够在列表视图中显示每个项目的位置和评级。到目前为止,这是我的代码

public class ResterauntList extends Activity {
    String cValue = null;
    String lValue = null;
    String rValue = null;
    ProgressDialog mProgressDialog;

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

        Bundle bdl = getIntent().getExtras();
        cValue = bdl.getString("cValue");
        lValue = bdl.getString("lValue");
        rValue = getIntent().getStringExtra("restrauntName");
        pickMethod();
    }

    @SuppressLint("DefaultLocale")
    public void pickMethod() {
        if (cValue == null) {
            if (lValue == null) {
                populateList(rValue, "name");
            } else {
                populateList(lValue.toLowerCase(), "area");
            }
        } else {
            populateList(cValue, "cuisine");
        }
    }

    private void populateList(final String Value, final String Key) {
        ParseQueryAdapter.QueryFactory<ParseObject> factory = new ParseQueryAdapter.QueryFactory<ParseObject>() {

            @Override
            @SuppressWarnings({ "unchecked", "rawtypes" })
            public ParseQuery create() {
                ParseQuery query = new ParseQuery("resdb");
                query.whereEqualTo(Key, Value).addAscendingOrder("name");
                return query;
            }
        };
        ParseQueryAdapter<ParseObject> adapter = new ParseQueryAdapter<ParseObject>(
                this, factory);
        adapter.setTextKey("name");
        adapter.setTextKey("area");
        adapter.addOnQueryLoadListener(new OnQueryLoadListener<ParseObject>() {

            @Override
            public void onLoading() {
                mProgressDialog = new ProgressDialog(ResterauntList.this);
                mProgressDialog.setTitle(Value + " Restaurants Search");
                mProgressDialog.setMessage("Loading...");
                mProgressDialog.setIndeterminate(false);
                mProgressDialog.show();
            }

            @Override
            public void onLoaded(List<ParseObject> objects, Exception e) {
                mProgressDialog.dismiss();
            }
        });

        final ListView listView = (ListView) findViewById(R.id.restListView);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                ParseObject object = (ParseObject) listView
                        .getItemAtPosition(position);
                String Id = object.getObjectId();
                Intent i = new Intent(getApplicationContext(),
                        SingleRestraunt.class);
                i.putExtra("restId", Id);
                startActivity(i);
            }
        });
    }

这是xml文件。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.gastronomaapp.ResterauntList" >

    <ListView
        android:id="@+id/restListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:textSize="40sp" >
    </ListView>

</RelativeLayout>

我已经阅读了一些关于自定义列表视图的教程,并且对一般情况下如何理解它有点了解,但我似乎无法弄清楚我应该在我的代码中进行哪些更改。有人可以告诉我如何专门针对我的上述代码吗?

1 个答案:

答案 0 :(得分:-1)

初学者的好教程:http://hmkcode.com/android-custom-listview-items-row/

只需复制粘贴行布局,看看getView方法如何...