Nonjoe
Nonjoe

Reputation: 121

Android onclick listener custom listview

With the following code I'm correctly receiving a dynamic list from mysql db and putting the elements in a listview.

public class MenuActivity extends ListActivity implements FetchDataListener {
private ProgressDialog dialog;

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

private void initView() {
    // show progress dialog
    dialog = ProgressDialog.show(this, "", "Loading..");

    String url = "http://www.*********.php";
    FetchDataTask task = new FetchDataTask(this);
    task.execute(url);
}

@Override
public void onFetchComplete(List<Application> data) {
    // dismiss the progress dialog
    if(dialog != null)  dialog.dismiss();
    // create new adapter
    ApplicationAdapter adapter = new ApplicationAdapter(this, data);

    // set the adapter to list
    setListAdapter(adapter);


}

@Override
public void onFetchFailure(String msg) {
    // dismiss the progress dialog
    if(dialog != null)  dialog.dismiss();
    // show failure message
    Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}

This is my array adapter:

public class ApplicationAdapter extends ArrayAdapter<Application>{
private List<Application> items;

public ApplicationAdapter(Context context, List<Application> items) {
    super(context, R.layout.app_cat_list, items);
    this.items = items;
}

@Override
public int getCount() {
    return items.size();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;

    if(v == null) {
        LayoutInflater li = LayoutInflater.from(getContext());
        v = li.inflate(R.layout.app_cat_list, null);
    }


    Application app = items.get(position);

    if(app != null) {

        TextView titleText = (TextView)v.findViewById(R.id.titleTxt);

        if(titleText != null) titleText.setText(app.getTitle());

           }

    return v;
}

Now I want to click on single row and open another activity passing some values via intent extra. Where should I implement click listener? I'm pretty sure it should be inserted in the "getView" but how I pass the app.getTitle() via intent? I know how pass intent extra in general, tried but no click happens. Any help would be appreciated, thanks

Upvotes: 0

Views: 253

Answers (3)

varun bhardwaj
varun bhardwaj

Reputation: 1522

There can be multiple ways and following is one of them:

Set onItemClickListner on your listview in your activity and it will give you a callback i.e onListItemClick. But as you said you want the title their you have to set tag on the convertView in the getView method like covertView.setTag("itemTitle"); and in your onListItemClick get the tag from view and convert it to the title like this v.getTag().toString(); and set it any where you want. follwoing is the full code:

@Override
public void onListItemClick(ListView l, View view, int position, long id) {
    String title = view.getTag().toString();
    Intent intent = new Intent(this, SecondActivity.class);
    intent.putExtra("title", title);
    startActivity(intent);
 // your code here... }

Please post if got stuck anywhere.

Upvotes: 0

Etun
Etun

Reputation: 403

Put this in your getView()

  v.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        Intent intent =new Intent(context, YourActivity.class);
                        context.startActivity(intent);

                    }
                });

Upvotes: 0

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132972

Now I want to click on single row and open another activity passing some values via intent extra. Where should I implement click listener?

No need to add OnItemClickListener because extending ListActivity in MenuActivity so just override onListItemClick method for handing ListView row click:

@Override
 public void onListItemClick(ListView l, View view, int position, long id) {
     // your code here...
 }

how I pass the app.getTitle() via intent?

Get selected row TextView value in onListItemClick using view parameter:

TextView txtView=(TextView)v.findViewById(R.id.titleTxt);
String selectedText=txtView.getText().toString();

Use selectedText for sending value with Intent in Next Activity

Upvotes: 1

Related Questions