1

I have an android project that gets the values from a remote database. I'm using a listview in eclipse and loops whatever values that my JSON have. Here is my code:

Newsfeed.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();
        }
    }
}

Now my problem is, since the listview is being looped in, How do I create a listview screen with different content for each row?

Thank you in advance! :D

Lewis Oviedo
  • 21
  • 2
  • 5

2 Answers2

0

You need to make an activity which should be blue print for all the activity that will be invoked by ListView .You need to append some values to Intent that you'll be passed to the activity and based on this remaining component can be created dynamically .The way you are doing you will get stuck because the data are coming remote database which may vary time to time.But what I am saying to make a basic skeleton of Activity because most the component will remain same only some of the component may only change which you can add dynamically based on intent valuse

Shakeeb Ayaz
  • 6,200
  • 6
  • 45
  • 64
0

Hello based on the @Shakeeb Shaheen comment here I am trying to write a pseudo-code for you. At first create a xml layout and name it common_layout.xml which will use to show trans and subtrans name. Here is the code of this layout:

<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>

Create an activity for holding this layout. Now from the NewsFeed activity you have to pass the trans name and subtrans name value when user click on the each list item. You can do this

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

And then you have to grab these value in your common activity class. This post also can help you how to pass value between two activity.

Community
  • 1
  • 1
androidcodehunter
  • 21,567
  • 19
  • 47
  • 70
  • Thank you very much for your answer! Really appreciate this :D But what if for example I have 3 rows in my listview (Purchase Order, Purchase Receive and Purchase Transfer) and each of them have different contents, can I still use common_layout.xml for this? Or Will I also make three xml pages? – Lewis Oviedo Oct 11 '13 at 06:12
  • Making single layout or multiple layout is depends on you. You can handle this situation in both way. if you want to use this same layout,you have to use some tricks to do that. Let me explain, suppose that in this current layout we have two textview to show the trans and subtrans name. So add one more textview and initially set visibility false and in the receiving activity check if purchase order data available or not. If available set textview visibility true. Hope this make sense. – androidcodehunter Oct 11 '13 at 06:18