Reputation: 159
I have an interface designed as a ListView (but I can change if needed). Each row has 2 buttons: MS and DROP.
Clicking on the buttons should affect only the corresponding row. For example, on the last row (where int "3" is displayed), clicking on MS should call function1(rowPosition) and clicking on DROP should call function2(rowPosition).
I have 2 problems:
Here is the main code:
package com.thomas.calculation.app;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
public class Memory_listview extends Activity {
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.memory_listview);
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(new MyAdapter(this));
}
}
class MyAdapter extends BaseAdapter {
Context context;
ArrayList<String> singleRow_list;
MyAdapter(Context c) {
context = c;
singleRow_list = new ArrayList<String>();
for (int i=1; i<10; i++)
singleRow_list.add(i, Integer.toString(i) );
}
@Override
public int getCount() {
return singleRow_list.size();
}
@Override
public Object getItem(int i) {
return singleRow_list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
class MyViewHolder
{
TextView myTextView;
MyViewHolder(View view) {
myTextView = (TextView) view.findViewById(R.id.textView);
}
}
@Override
public View getView(int i, View view, ViewGroup viewGroup)
{
View view_single_row = view;
MyViewHolder myViewHolder = null;
if (view_single_row==null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view_single_row = inflater.inflate(R.layout.memory_listview_row, viewGroup, false);
myViewHolder = new MyViewHolder(view_single_row);
view_single_row.setTag(myViewHolder);
}
else
{
myViewHolder = (MyViewHolder) view_single_row.getTag();
}
String newData = singleRow_list.get(i);
//This is not working:
//Button button_MS = (Button) findViewById(R.id.button_MS); //ERROR: findViewById undefined
//Button button_MS = (Button) this.findViewById(R.id.button_MS); //ERROR: findViewById undefined
//Button button_MS = (Button) view.findViewById(R.id.button_MS); //APP CRASH
//Button button_MS = (Button) view_single_row.findViewById(R.id.button_MS); //APP CRASH
//Button button_MS = (Button) viewGroup.findViewById(R.id.button_MS); //APP CRASH
myViewHolder.myTextView.setText(newData);
return view_single_row;
}
}
Here is the row.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/bgStack"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView"
android:text=""
style="@style/memory_monitor"
android:weightSum="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="@+id/linearView" />
<LinearLayout
android:id="@+id/linearView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentRight="true">
<TextView
android:id="@+id/button_MS"
style="@style/memory_tiny"
android:text="@string/calc_MS" />
<TextView
android:id="@+id/button_del"
style="@style/memory_tiny"
android:text="@string/calc_del" />
</LinearLayout>
</RelativeLayout>
Upvotes: 2
Views: 352
Reputation: 6334
Inside your getView.
MyViewHolder.button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
// do for the other button too..
// yout methods here.
}
});
And change the TextView
to Button
in your xml
Upvotes: 1