Here is how i did onClick Listner event on button on ListView with startActivity inside BaseAdaptor.
main.xml
<?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="match_parent"
android:layout_height="wrap_content"
android:text="ListView Button Click Example" />
<ListView android:id="@+id/list_data"
android:layout_height="match_parent"
android:layout_width="match_parent" />
</LinearLayout>
linear_test.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layoutContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="5"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_weight="3"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/tv_question"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:text="Are you Sure want to make sure you can do this ?" />
</LinearLayout>
<LinearLayout
android:id="@+id/LinearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yes" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No" />
</LinearLayout>
</LinearLayout>
MainActivity.java
package com.example.dlistview;
import java.util.ArrayList;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.os.Build;
import android.app.Activity;
import android.view.ViewGroup.LayoutParams;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String TAG = MainActivity.class.getSimpleName();
ListView listView = (ListView) findViewById(R.id.list_data);
CustomAdapter customAdapter = new CustomAdapter();
listView.setAdapter(customAdapter);
}
}
dataModel.java
package com.example.dlistview;
public class DataModel {
private String name;
private int anInt;
public DataModel(String name, int anInt) {
this.name = name;
this.anInt=anInt;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAnInt() {
return anInt;
}
public void setAnInt(int anInt) {
this.anInt = anInt;
}
}
CustomAdaptor.java
package com.example.dlistview;
import java.util.ArrayList;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class CustomAdapter extends BaseAdapter {
private static final String TAG = CustomAdapter.class.getSimpleName();
ArrayList<DataModel> listArray;
public CustomAdapter() {
listArray = new ArrayList<DataModel>(5);
listArray.add(new DataModel("Class 1",1));
listArray.add(new DataModel("Class 2",2));
listArray.add(new DataModel("Class 3",3));
listArray.add(new DataModel("Class 4",4));
listArray.add(new DataModel("Class 5",5));
}
@Override
public int getCount() {
return listArray.size(); // total number of elements in the list
}
@Override
public Object getItem(int i) {
return listArray.get(i); // single item in the list
}
@Override
public long getItemId(int i) {
return i; // index number
}
@Override
public View getView(int index, View view, final ViewGroup parent) {
if (view == null) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
view = inflater.inflate(R.layout.linear_test,parent,false);
}
final DataModel dataModel = listArray.get(index);
TextView textView = (TextView) view.findViewById(R.id.tv_question);
textView.setText(dataModel.getName());
Button btnyes = (Button) view.findViewById(R.id.button2);
Button btnno = (Button) view.findViewById(R.id.button1);
btnyes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch( dataModel.getAnInt()){
case 1:
Intent intent = new Intent(parent.getContext(),Yes1.class);
parent.getContext().startActivity(intent);
case 2:
Toast.makeText(parent.getContext(), "Button Clicked"+ dataModel.getName(),Toast.LENGTH_LONG).show();
case 3:
Toast.makeText(parent.getContext(), "Button Clicked"+ dataModel.getName(),Toast.LENGTH_LONG).show();
default:
}
}
});
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d(TAG, "string: " + dataModel.getName());
Toast.makeText(parent.getContext(), "view clicked: " + dataModel.getName(), Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
Here is the screenshot
Subscribe to:
Post Comments
(
Atom
)

No comments :
Post a Comment