0

I used SQLiteAssetHelper. I connected SQLite database to connect to my populate_list.xml. Buttons gets sub_products column from my table. I am trying to get the ID of button. When I used ViewFindById(name_of_button). It returns null. I am thinking maybe I need refresh but I don't exactly know how to do that.

This is my CoreOpenDB.class. This is not Mainactivity

public class CoreOpenDB  extends ListActivity {

private Cursor products;
private MyDatabase db;

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Bundle b = getIntent().getExtras();
    String value = "test"; // or other values
    if(b != null)
        value = b.getString("key");


    db = new MyDatabase(this);
    products = db.getProducts(value); 

    ListAdapter adapter = new SimpleCursorAdapter(this,
            R.layout.populate_list,
           products,
            new String[] {"sub_products"},
            new int[] {R.id.text_list});

    getListView().setAdapter(adapter)
}

This is populate_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
android:id="@+id/text_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Sample Product"
android:padding="25px"
android:textColor="#333"
android:textSize="38dp" />
</LinearLayout>

And this is the result enter image description here

I need to get id's of each item. Thank you .

OneCricketeer
  • 126,858
  • 14
  • 92
  • 185
a_bldmr
  • 47
  • 11
  • You can get the row view from the adapted. – OneCricketeer Jun 23 '17 at 05:40
  • What exactly are you trying to do? Why do you need to have buttons rather than TextView ? – OneCricketeer Jun 23 '17 at 05:42
  • I need the buttons to make actions if they are clicked. That will transfer to another sub category of each item until it goes to the single item that shows its details. @cricket_007 – a_bldmr Jun 23 '17 at 05:44
  • I'll try the row view from the adapter. btw, im searching ListAdapter documentation – a_bldmr Jun 23 '17 at 05:45
  • 1
    instead of ` – pskink Jun 23 '17 at 05:46
  • @pskink . do you mean I code getListView().onItemClickListener ? Sorry I'm new to this. I don't exactly understand the setup part – a_bldmr Jun 23 '17 at 05:49
  • getListView().setOnItemClickListener(...) – pskink Jun 23 '17 at 05:50
  • OK. i'll be back in a bit. – a_bldmr Jun 23 '17 at 05:51
  • I'm sorry. What parameter is needed for getListView().setOnItemClickListener()? – a_bldmr Jun 23 '17 at 06:00
  • and what do `AdapterView#setOnItemClickListener` docs say? – pskink Jun 23 '17 at 06:04
  • ok i'll read it. ur a good teacher. in a bit. – a_bldmr Jun 23 '17 at 06:07
  • final Object[] listItem = new Object[1]; getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView> parent, View view, int position, long id) { listItem[0] = getListView().getItemAtPosition(position); } });//getListView().getOnItemClickListener( I did this but it returns null – a_bldmr Jun 23 '17 at 06:24
  • 1
    what returns null? `getItemAtPosition(position)` returns null? why do you want to call `getItemAtPosition`? – pskink Jun 23 '17 at 06:31
  • https://stackoverflow.com/questions/24811536/android-listview-get-item-view-by-position for reference. Got what I need. – a_bldmr Jun 23 '17 at 06:49
  • no, no. no, `getChildAt` has nothing to do with the adapter, what do you want to achieve actually when clicking on list view item? – pskink Jun 23 '17 at 06:53
  • I don't know how to get the Button element. The link made me get button in linearlayout of xml. After I got Button element, I will put action when specific button is clicked. – a_bldmr Jun 23 '17 at 07:42
  • 2
    what do you need Button for? instead R.layout.populate_list use for example android.R.layout.simple_list_item_1 which has one nicely formatted `TextView` – pskink Jun 23 '17 at 09:45
  • What we are trying to tell you is that you can click on literally anything to perform an action. Buttons like in the picture look strange and a simple TextView look cleaner (plus you do not need to find the button in order to set a click listener on it) – OneCricketeer Jun 23 '17 at 13:04
  • It looks weird because I'm not making the ui front-end at the moment. I'm trying to make the back end work first. Can I know where the user clicked if I used the method that catches when the user clicks on anything? – a_bldmr Jun 24 '17 at 03:25
  • `geListView().setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView> adapter, View view, int position, long id) { Log.d("test:", "test"); } }); ` I tried this but it does not produce. The Log – a_bldmr Jun 24 '17 at 03:43
  • 1
    did you use a std `android.R.layout.simple_list_item_1` as your layout resource in `SCA` constructor? – pskink Jun 24 '17 at 05:33
  • guys. I used android.R.Id.simple_list_item_1 and your recommendations work. answer and I will mark as correct answer – a_bldmr Jun 24 '17 at 05:38
  • 1
    so now you see how simple it was, wasn't it? – pskink Jun 24 '17 at 07:11
  • yeah. haha. thank you for your patience. – a_bldmr Jun 24 '17 at 08:41

0 Answers0