我有一个带自定义适配器的ListView。它显示左侧的图像和两行文字。
mylist.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="fill_parent" android:orientation="horizontal">
<ImageView
android:id="@+id/icon"
android:layout_width="60dp"
android:layout_height="60dp"
android:padding="5dp" />
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:padding="2dp"
android:textColor="#33CC33" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_marginLeft="10dp"/>
</LinearLayout>
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<Switch
android:id="@+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="21dp"
android:layout_marginBottom="21dp" />
</LinearLayout>
我想设置动态文本来编辑id“textView1”的文本,但是这个textview将显示在listview的每一行上,我只想更改第二行的文本。
我在MainActivity.java中使用了这段代码
package com.chinmay.callblocker;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ListActivity {
private TextView extraTxt;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomListAdapter adapter = new CustomListAdapter(this, itemname, imgid, textDescription);
setListAdapter(adapter);
View inflatedView = getLayoutInflater().inflate(R.layout.mylist, null);
extraTxt = (TextView) inflatedView.findViewById(R.id.textView1);
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
final SharedPreferences.Editor e = settings.edit();
e.putBoolean("switch", false);
e.commit();
getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
/*String selected_item = "List View";
Toast.makeText(getBaseContext(), selected_item, Toast.LENGTH_SHORT).show();*/
if(position == 1) {
alert.setTitle("Message");
alert.setMessage("This message will be sent when call blocking is on");
// Set an EditText view to get user input
final EditText input = new EditText(getBaseContext());
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString();
extraTxt.setText(value);
//Toast.makeText(getBaseContext(), value, Toast.LENGTH_LONG).show();
// Do something with value!
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
}
}
});
}
public void onListItemClick(ListView lv ,View view,int position,int imgid) {
/*String selected_item = "List View";
Toast.makeText(this, selected_item, Toast.LENGTH_SHORT).show();*/
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
CustomListAdapter.java
package com.chinmay.callblocker;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
/**
* Created by user on 11/4/2014.
*/
public class CustomListAdapter extends ArrayAdapter<String> {
private final Activity context;
private final String[] itemname;
private final Integer[] imgid;
private final String[] textDescription;
AudioManager mAudio;
public CustomListAdapter(Activity context, String[] itemname, Integer[] imgid, String[] textDescription) {
super(context, R.layout.mylist, itemname);
// TODO Auto-generated constructor stub
this.context=context;
this.itemname=itemname;
this.imgid=imgid;
this.textDescription = textDescription;
}
public View getView(int position,View view,ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.mylist, null,true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.item);
final Switch mSwitch = (Switch) rowView.findViewById(R.id.switch1);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
TextView extra_txt = (TextView) rowView.findViewById(R.id.textView1);
txtTitle.setText(itemname[position]);
imageView.setImageResource(imgid[position]);
extra_txt.setText(textDescription[position]);
mAudio = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
if(position == 0) {
imageView.setVisibility(View.GONE);
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
final SharedPreferences.Editor e = settings.edit();
mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
if(isChecked) {
mAudio.setRingerMode(AudioManager.RINGER_MODE_SILENT);
e.putBoolean("switch", mSwitch.isChecked());
e.commit();
} else {
mAudio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
e.putBoolean("switch", mSwitch.isChecked());
e.commit();
}
}
});
} else if(position != 0) {
mSwitch.setVisibility(View.GONE);
if(position == 1) {
}
}
return rowView;
};
}
现在这段代码不起作用,因为显然无法获取第2行的文本视图然后更改它。但我认为必须有一些方法来处理ListView上的这些问题。
答案 0 :(得分:0)
您需要在CustomListAdapter上调用notifyDataSetChanged()