我有这段代码
public void onKillClick(View v) {
TextView t = (TextView) v.findViewById(R.id.toptext);
String s = t.getText().toString();
net.killProcess(s);
}
row.xml
中的按钮将onKillClick
作为其收听者。
t.getText().toString()
返回null。我不确定我是否添加了正确的onClickListener
或者什么。
Client.java
package org.tsunamistudios.computerwatch;
import java.io.IOException;
import java.io.OptionalDataException;
import java.util.ArrayList;
import java.util.Collection;
import org.tsunamistudios.computerwatch.net.Net;
import org.tsunamistudios.computerwatch.processes.Program;
import android.app.ListActivity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.tsunamistudios.computerwatch.R;
public class Client extends ListActivity {
private static String message;
private static ArrayList<Program> programs = new ArrayList<Program>();
private static Net net = new Net();
private static TextView txtView;
private static ProgramAdapter prAdapter;
private static Context context;
private static ArrayList<TextView> textViews = new ArrayList<TextView>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
context = getApplicationContext();
}
private class GetObject extends AsyncTask<Void, Void, String> {
@SuppressWarnings("unchecked")
@Override
protected String doInBackground(Void... arg0) {
net.setSocket();
try {
getPrograms().addAll((Collection<? extends Program>) net.getObjectInputStream().readObject());
} catch (OptionalDataException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String result) {
prAdapter = new ProgramAdapter(context, R.layout.row, programs);
setListAdapter(prAdapter);
}
}
private class KillProcess extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... parems) {
net.killProcess(parems[0]);
return null;
}
}
public void onKillClick(View v) {
String s = textViews.get(v.getId()).getText().toString();
net.killProcess(s);
}
private class ProgramAdapter extends ArrayAdapter<Program> {
private ArrayList<Program> programs;
public ProgramAdapter(Context context, int textViewResourceId, ArrayList<Program> programs) {
super(context, textViewResourceId, programs);
this.programs = programs;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
Program p = programs.get(position);
if (p != null) {
Bitmap bmp = BitmapFactory.decodeByteArray(p.getImage(), 0, p.getImage().length);
TextView tt = (TextView) v.findViewById(R.id.toptext);
TextView bt = (TextView) v.findViewById(R.id.bottomtext);
ImageView im = (ImageView) v.findViewById(R.id.icon);
im.setImageBitmap(bmp);
if (tt != null) {
tt.setText("Name: "+ p.getName().substring(0, p.getName().length() - 4));
textViews.add(tt);
}
if(bt != null) {
bt.setText("Desc: "+ p.getDescription());
}
}
return v;
}
}
public void onIpClick(View v){
EditText et = (EditText) findViewById(android.R.id.empty);
net.setHostName(et.getText().toString());
new GetObject().execute((Void) null);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.client, menu);
return true;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
Client.message = message;
}
public static ArrayList<Program> getPrograms() {
return programs;
}
public static void setTextView() {
txtView.setText(getPrograms().get(0).getName() + getPrograms().get(0).getDescription());
}
public void setPrograms(ArrayList<Program> programs) {
Client.programs = programs;
}
public Net getNet() {
return net;
}
public void setNet(Net net) {
Client.net = net;
}
public static TextView getTxtView() {
return txtView;
}
}
我的activity_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"
>
<ListView
android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<TextView
android:id="@+id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/main_no_items"/>
</LinearLayout>
row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="6dip"
android:src="@drawable/ic_launcher"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="fill_parent">
<TextView
android:id="@+id/toptext"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="center_vertical"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:id="@+id/bottomtext"
android:singleLine="true"
android:ellipsize="marquee"
/>
<Button
android:id="@+id/button1"
android:layout_height = "wrap_content"
android:layout_width ="wrap_content"
android:text = "Enter"
android:onClick = "onKillClick"
android:gravity="right"
/>
</LinearLayout>
</LinearLayout>
答案 0 :(得分:0)
尝试在导致button1.setOnClickListner(listner)
的{{1}}内添加Adapter
。你会得到这样的东西:
row.xml
答案 1 :(得分:0)
TextView t = (TextView) v.findViewById(R.id.toptext);
在getView
(适配器)或onCreate(活动)中声明此行。
在onKillClick
函数内部,v是此控件(按钮),不包含顶部文本Textview。
答案 2 :(得分:0)
尝试以下方法(使用膨胀视图)
public void onKillClick(View v) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//or youractivity.getsystemservice
v = vi.inflate(R.layout.row, null);
TextView t = (TextView) v.findViewById(R.id.toptext);
String s = t.getText().toString();
net.killProcess(s);
}