tags:

views:

2063

answers:

4

Hi, android is new for me. I am trying to develop a program on 1.5 platform but still in progress, plz guide me.

I have some information in following format

"item1","description1"
"item2","description2"
"item3","description3"
"item4","description4"
.
.
.
.

I want to show them on screen, I dont know which is recommended way to do this. After google I found 2 method. but I failed to successfully implement any of one.

Method 1

I break both columns data into 2 different arrays, then populate listactivity with array of column 1, enable filter & on clicked event I want to raise alert which should show Text clicked in tilte & desc from 2nd array as msg body based on position. But here is problem if using filter index becomes reinitialize :-(, and there didnot find another way to get text of that row.


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.main);

   setListAdapter(new ArrayAdapter<String>(this, 
                               android.R.layout.simple_list_item_1, Names));    
   getListView().setTextFilterEnabled(true);

}


public void onListItemClick(ListView parent, View v, int position, long id) {

    Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(Names[position]); 
    builder.setMessage(description[position] + " -> " + position );
    builder.setPositiveButton("ok", null);
    builder.show();
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
</LinearLayout>


its not picking right item from position if filter used :-(, plz guide Can you share source code of this

Method B

Here I tried to generate list row from XML , but giving error that 1.5 jar file didnot allow modification :-(


    public View getView(int position, View convertView, ViewGroup parent) {

            /*       
            ViewInflate inflater=context.getViewInflate();
            View row=inflater.inflate(R.layout.row, null, null);
            */

            View row = (View) convertView;

            if (row==null) {
                LayoutInflater  inflater=context.getLayoutInflater();
            //    LayoutInflater inflater = (LayoutInflater)  context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
                  row = inflater.inflate(R.layout.row,null);
            }

            TextView label=(TextView)row.findViewById(R.id.label);
            label.setText(items[position]);

            TextView description=(TextView)row.findViewById(R.id.description);
            description.setText(items[position]);

            //    ImageView icon=(ImageView)row.findViewById(R.id.icon);
            //    icon.setImageResource(R.drawable.delete);


            return(row);
        }


Plz suggest what is a right way to acconplish this, so app can show desc of item , has filter too. Plz share If you have any shouce code for this

+2  A: 
Ramps
A: 

Ramps,

Your explanation was so much clearer and detailled than everything I found before that I manage to set the damn filter I was banging my head on for 2 weeks now... Because, as you mentionned here, I was dealing with objects from which I "get" some elements to display in textviews, put together in one view of the list. And I spotted your "@Override toString ..." and it just pop in my head that I had to Override the toString of my objects to let the filter analyse the text I want it to filter. And it worked perfectly. Thx SO MUCH I'll give you +1 (would be +10 if I could ) and put some links one other topics toward yours!

Sephy
A: 

Hi Sephy,

Could you possibly expand on the code you used to get your filter working? I think I've got a similar issue, and I'm wondering whether I need to subclass my adapter or whether I can do it with the existing SimpleCursorAdapter methods.

Thanks

Steve

Stev_k
Actually, I figured it out - I wasn't changing the cursor on the Adapter after running runQuery. Nothing to do with the text at all.Thanks
Stev_k
A: 

Hi Ramps,

Could you please help me in filtering a ListView ? Actually I am subclassing ArrayAdapter<File>. I also don't know how to call/implement getFilter().filter() method.

Kaillash