1

我有一个从远程数据库获取值的 android 项目。我在 eclipse 中使用 listview 并循环我的 JSON 具有的任何值。这是我的代码:

新闻提要.java

public class NewsFeed extends ListActivity {

    // Progress Dialog
    private ProgressDialog pDialog;


    private static final String READ_COMMENTS_URL = "http://10.0.2.2/PMR_Drupal/newsfeed.php";

    private static final String TAG_TITLE = "trans_name";
    private static final String TAG_FOR_A = "sub_trans_name";
    private static final String TAG_ACCESS_LEVEL = "ACCESS_LEVEL";

    private JSONArray mComments = null;
    //manages all of our comments in a list.
    private ArrayList<HashMap<String, String>> mCommentList;

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_activity_actions, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        //loading the comments via AsyncTask
        new LoadComments().execute();
    }

    public void addComment(View v)
    {
        Intent i = new Intent("com.pallet.pts.ADDNEWSFEED");
       startActivity(i);
    }


        public void updateJSONdata() {


            mCommentList = new ArrayList<HashMap<String, String>>();


            JSONParser jParser = new JSONParser();

            JSONObject json = jParser.getJSONFromUrl(READ_COMMENTS_URL);

            try {


                mComments = json.getJSONArray(TAG_POSTS);


                for (int i = 0; i < mComments.length(); i++) {
                    JSONObject c = mComments.getJSONObject(i);


                    String trans_name = c.getString(TAG_TITLE);
                    String sub_trans_name = c.getString(TAG_FOR_A);
                    String ACCESS_LEVEL = c.getString(TAG_ACCESS_LEVEL);

                    HashMap<String, String> map = new HashMap<String, String>();

                    map.put(TAG_TITLE, trans_name);
                    map.put(TAG_FOR_A, sub_trans_name);
                    map.put(TAG_ACCESS_LEVEL, ACCESS_LEVEL);

                    mCommentList.add(map);

                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }


            private void updateList() {

                ListAdapter adapter = new SimpleAdapter(this, mCommentList,
                        R.layout.single_post, new String[] { TAG_TITLE, TAG_FOR_A, TAG_ACCESS_LEVEL}, new int[] { R.id.title, R.id.forApproval, R.id.count});


                setListAdapter(adapter);


                ListView lv = getListView();    
                lv.setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {


                         Intent intent = new Intent("com.pallet.pts.NEWSFEED_CLICKED");
                            intent.putExtra("position", position);
                            startActivity(intent);

                    }
                });
            } 

    public class LoadComments extends AsyncTask<Void, Void, Boolean> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(NewsFeed.this);
            pDialog.setMessage("Checking for Updates...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }
        @Override
        protected Boolean doInBackground(Void... arg0) {
            //we will develop this method in version 2
            updateJSONdata();
            return null;

        }


        @Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);
            pDialog.dismiss();
          //we will develop this method in version 2
            updateList();
        }
    }
}

现在我的问题是,由于列表视图正在循环中,如何为每一行创建一个具有不同内容的列表视图屏幕?

先感谢您!:D

4

2 回答 2

0

您好,根据@Shakeeb Shaheen 的评论,我正在尝试为您编写伪代码。首先创建一个 xml 布局并将其命名为 common_layout.xml,用于显示 trans 和 subtrans 名称。这是此布局的代码:

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

    <TextView
        android:id="@+id/tv_trans_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/tv_subtrans_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

创建一个活动来保存这个布局。现在从 NewsFeed 活动中,当用户单击每个列表项时,您必须传递 trans name 和 subtrans name 值。你可以这样做

Intent intent = new Intent(NewsFeed.this, CommonActivity.class);
intent.putExtra("TRANS_NAME", trans_name);
intent.putExtra("SUBTRANS_NAME", subtrans_name);
startActivity(intent);

然后你必须在你的公共活动类中获取这些价值。这篇文章还可以帮助您如何在两个活动之间传递价值

于 2013-10-11T05:50:15.813 回答
0

您需要创建一个应该blue print for all the activity由 ListView 调用的活动。您需要向 Intent 附加一些值,您将被传递给活动,并且可以基于此剩余组件动态创建。您正在做的方式会卡住,因为数据来自远程数据库,可能会不时变化。但我所说的是基本skeleton of Activity的,因为大多数组件将保持不变,只有一些组件可能只会更改,您可以根据意图值动态添加

于 2013-10-11T04:27:18.057 回答