Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to print data in a ListView using ListAdapter
    primarykey
    data
    text
    <p><strong>Updated</strong></p> <p>I'm trying to print the data retrieved from the database on a list view. For a while my application print for each data in the database, a row on the list view. So if there are 10 data in the database, the app prints 10 rows, corresponding to each row of the database. Here is a view of how it is being printed.</p> <p>The new image of how it looks now:</p> <p><img src="https://i.stack.imgur.com/5WN59.png" alt="alt text"></p> <p>But, it is not printing the data as I want. I want to print a column of the row in a specific text view, but it doesnt show anything.</p> <p>So the activity RatedCalss calls the method <code>selectTopCalls()</code> and makes a List receive the list that this method returns. And then I pass this list to the listAdapter.</p> <p>Well I have this activity named <strong>RatedCalls.java</strong>:</p> <pre><code>public class RatedCalls extends Activity { private static final String LOG_TAG = "RatedCalls"; private CallDataHelper cdh; private ListView listview; private ArrayList&lt;String&gt; ratedCallsList; private MyListAdapter listAdapter; private View view; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Log.i(LOG_TAG, "calling from onCreate()"); cdh = new CallDataHelper(this); startService(new Intent(this, RatedCallsService.class)); setBasicContent(); } public void setBasicContent() { listview = (ListView) findViewById(R.id.list_view); ratedCallsList = this.cdh.selectTopCalls(); Log.i(LOG_TAG, "ratedCallsList size: " + ratedCallsList.size()); listAdapter = new MyListAdapter(this, this, R.id.list_view, ratedCallsList); listview.setAdapter(listAdapter); } } </code></pre> <p>I have this class, a ListAdapter class named <strong>MyListAdapter.java</strong>:</p> <pre><code> public class MyListAdapter extends ArrayAdapter { //--CloneChangeRequired private ArrayList mList; private Context mContext; private Activity mActivity; private int selectedPos = -1; // init value for not-selected private ArrayList&lt;String&gt; ratedCallsList; private CallDataHelper cdh; public void setSelectedPosition(int pos){ selectedPos = pos; notifyDataSetChanged(); } public int getSelectedPosition(){ return selectedPos; } public MyListAdapter(Context context, Activity activity, int textViewResourceId, ArrayList list) { super(context, textViewResourceId, list); this.mList = list; this.mContext = context; this.mActivity = activity; } public View getView(int position, View convertView, ViewGroup parent){ View view = convertView; try{ if (view == null) { LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = vi.inflate(R.layout.list_item, null); // --CloneChangeRequired(list_item) } // setting STRIP BG if(position == selectedPos){ view.findViewById(R.id.rlt_main).setBackgroundColor( Color.rgb(062, 076, 120) ); }else if(position%2==0){ view.findViewById(R.id.rlt_main).setBackgroundColor( Color.rgb(226, 231, 239) ); }else{ view.findViewById(R.id.rlt_main).setBackgroundColor( Color.rgb(200, 210, 223) ); } setViews(position, view); }catch(Exception e){ //Log.i(MyListAdapter.class.toString(), e.getMessage()); } return view; } public void setViews(int position, View view) { cdh = new CallDataHelper(mContext); if(mContext.getClass().equals((RatedCalls.class))){ ratedCallsList = this.cdh.selectTopCalls(); Log.i("MYLISTADAPTER", "size " + ratedCallsList.size()); if (ratedCallsList != null) { ((TextView) view.findViewById(R.id.contact_name)).setText(ratedCallsList.get(0)); ((TextView) view.findViewById(R.id.phone_number)).setText(ratedCallsList.get(1)); ((TextView) view.findViewById(R.id.duration)).setText(ratedCallsList.get(2)); ((TextView) view.findViewById(R.id.date)).setText(ratedCallsList.get(3)); } }else if(mContext.getClass().equals(RatedContacts.class)){ final PublishersBO listPublisher = (PublishersBO) mList.get(position); if (listPublisher != null) { //--setting list_item views ((TextView) view.findViewById(R.id.contact_name)).setText(listPublisher.getName()); ((TextView) view.findViewById(R.id.phone_number)).setText(listPublisher.getEmail()); //--onClickListener view.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { Intent myIntent = new Intent(mContext,RatedContacts.class); myIntent.putExtra("NAME", listPublisher.getName()); myIntent.putExtra("ACTIVITY_NAME", mContext.getClass().toString()); mContext.startActivity(myIntent); mActivity.finish(); } }); } } } } </code></pre> <p>The method that retrieve the data from the database is in a separated class that deals with SQLite function. This is the method:</p> <pre><code>public ArrayList&lt;String&gt; selectTopCalls() { ArrayList&lt;String&gt; list1 = new ArrayList&lt;String&gt;(); Cursor cursor = this.db.query(TABLE_NAME, null, null, null, null, null, "duration desc"); if (cursor.moveToFirst()) { do { //if (cursor.getString(2) != "") { cdObj = new CallData(); list1.add(cursor.getString(2)); list1.add(cursor.getString(4)); list1.add(cursor.getString(5)); list1.add(cursor.getString(6)); } while (cursor.moveToNext()); } if (cursor != null &amp;&amp; !cursor.isClosed()) { cursor.close(); } return list1; } </code></pre> <p>And here is the xml file for the view named <strong>list_item.xml</strong>:</p> <p> </p> <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="67px" android:id="@+id/rlt_main" android:layout_toRightOf="@+id/iv_forward"&gt; &lt;ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:id="@+id/iv_forward" android:src="@drawable/icon" android:layout_alignParentLeft="true"&gt; &lt;/ImageView&gt; &lt;TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/phone_number" android:layout_alignParentBottom="true" android:layout_toRightOf="@+id/iv_forward" android:layout_alignBottom="@+id/iv_forward"&gt; &lt;/TextView&gt; &lt;TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/duration" android:layout_alignBottom="@+id/phone_number" android:layout_alignRight="@+id/phone_number" &gt; &lt;/TextView&gt; &lt;TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/date" android:layout_alignBottom="@+id/contact_name" android:layout_alignRight="@+id/contact_name" &gt; &lt;/TextView&gt; &lt;TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/contact_name" android:textSize="20px" android:layout_toRightOf="@+id/iv_forward" android:layout_alignParentTop="true"&gt; &lt;/TextView&gt; </code></pre> <p> </p> <p>So I'd like to print the data in the text views in the activity, but I dont know where to set the text, in what class, if in the MyListAdapter class or if in the activity.</p> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload