我在android开发中很新鲜。我开始学习并通过listview中的可点击按钮停止工作!
`public class Test2 extends AppCompatActivity {
private static final String TAG = "Test2";
Button btnEdytuj;
DatabaseHelper myDb;
SimpleCursorAdapter dataAdapter;`
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test2);
myDb = new DatabaseHelper(this);
displayList();
}
private void displayList() {
Cursor cursor = myDb.getAllData();
String from[] = new String[]{myDb.COL_ID, myDb.COL_NAZWA, myDb.COL_LAT, myDb.COL_LON};
int to[] = new int[]{R.id.textView10, R.id.textView1, R.id.textView2, R.id.textView3};
dataAdapter = new SimpleCursorAdapter(this, R.layout.row_item_edit, cursor, from, to, 0);
ListView lv = (ListView) findViewById(R.id.listViewId);
lv.setAdapter(dataAdapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
btnEdytuj = (Button) view.findViewById(R.id.buttonEE);
btnEdytuj.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
// Do something
Intent intent = new Intent(Test2.this,Edytuj.class);
startActivity(intent);`
我的愿望是通过按下一行或按钮并传递一个id来处理编辑,从意图触发器列表中打开新活动。
我发现了一个错误,可能有助于弄明白:
android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
答案 0 :(得分:1)
执行此操作:
这是获取视图功能:
private TextToSpeech tts;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tts = new TextToSpeech(this, this);
((Button) findViewById(R.id.bSpeak)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
speakOut(((TextView) findViewById(R.id.tvTranslatedText)).getText().toString());
}
});
((Button) findViewById(R.id.bTranslate)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
class bgStuff extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
try {
if (params.length > 0) {
return translate(params[0]);
}
} catch (Exception e) {
e.printStackTrace();
return e.toString();
}
return null;
}
@Override
protected void onPostExecute(String result) {
((TextView) findViewById(R.id.tvTranslatedText)).setText(result);
}
}
new bgStuff().execute(((EditText) findViewById(R.id.etUserText)).getText().toString());
}
});
}
public String translate(String text) throws Exception {
// Set the Client ID / Client Secret once per JVM. It is set statically and applies to all services
Translate.setClientId("CLIENT ID"); //Change this
Translate.setClientSecret("CLIENT SECRET"); //change
String translatedText = "";
translatedText = Translate.execute(text, Language.GERMAN);
return translatedText;
}
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.GERMAN);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
//speakOut("Ich");
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
private void speakOut(String text) {
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
另一个用于绑定视图:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.list_note, null);
btnEdytuj = (Button) convertView.findViewById(R.id.buttonEE);
btnEdytuj.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
// Do something
}
});
}
return convertView;
}
答案 1 :(得分:0)
你走了:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//Write your code here
}
});
希望这有帮助。
答案 2 :(得分:0)
您必须将onClick
方法按钮放在Adapter
类中。
使用SimpleCursorAdapter
时,请使用button-inside-simplecursoradapter作为参考。
答案 3 :(得分:0)
请在您的活动类中编写以下代码。
<强> MainActivity.java 强>
ArrayList<MobileData> searchResults = getMobileData();
final ListView lv1 = (ListView) findViewById(R.id.ListView01);
lv1.setAdapter(new MobileAdapter(this, searchResults));
使用适配器类添加所有列表项,并在adaptaer类中添加按钮事件。
<强> MobileAdapter.java 强>
public class MobileAdapter extends BaseAdapter {
private static ArrayList<MobileData> searchArrayList;
private LayoutInflater mInflater;
private Context context;
MobilezonDAO dao;
public MobileAdapter(Context context, ArrayList<MobileData> results) {
this.context = context;
searchArrayList = results;
mInflater = LayoutInflater.from(context);
dao = new MobilezonDAO(this.context);
}
public int getCount() {
return searchArrayList.size();
}
public Object getItem(int position) {
return searchArrayList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.mobile_row_view, null);
holder = new ViewHolder();
holder.mobileImage = (ImageView) convertView.findViewById(R.id.mobileImage);
holder.mobileName = (TextView) convertView.findViewById(R.id.mobileName);
holder.mobilePrice = (TextView) convertView.findViewById(R.id.mobilePrice);
holder.mobileBrand = (TextView) convertView.findViewById(R.id.mobileBrand);
holder.viewDetails = (Button) convertView.findViewById(R.id.viewDetails);
holder.addToCart = (Button) convertView.findViewById(R.id.addToCart);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.mobileImage.setImageBitmap(searchArrayList.get(position).getMobileImage());
holder.mobileName.setText(searchArrayList.get(position).getMobileName());
holder.mobilePrice.setText("Price: $"+searchArrayList.get(position).getMobilePrice());
holder.mobileBrand.setText("Brand: "+searchArrayList.get(position).getMobileBrand());
holder.viewDetails.setBackgroundResource(R.drawable.view_details);
holder.viewDetails.setId(position);
holder.addToCart.setBackgroundResource(R.drawable.addtocart);
holder.viewDetails.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MobileData mobileData = searchArrayList.get(holder.viewDetails.getId());
Intent intent= new Intent(context, MobileViewDetailsActivity.class);
intent.putExtra("ModelNumber", mobileData.getModelNumber());
context.startActivity(intent);
}
});
holder.addToCart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MobileData mobileData = searchArrayList.get(holder.viewDetails.getId());
CartData cartData = new CartData();
cartData.setUsername(emailid);
cartData.setModelNumber(mobileData.getModelNumber());
cartData.setPrice(mobileData.getMobilePrice());
cartData.setQuantity("1");
CartData tempCartData = dao.searchParticularCartData(cartData.getUsername(), cartData.getModelNumber());
if(tempCartData!=null && tempCartData.getQuantity()!=null && Integer.valueOf(tempCartData.getQuantity())>0){
Snackbar.make(view, "Selected Mobile already added in your cart.", Snackbar.LENGTH_LONG).setAction("Action", null).show();
}else{
dao.insertCartData(cartData);
Snackbar.make(view, "Mobile has been successfully added in your cart.", Snackbar.LENGTH_LONG).setAction("Action", null).show();
}
}
});
return convertView;
}
static class ViewHolder {
ImageView mobileImage;
TextView mobileName;
TextView mobilePrice;
TextView mobileBrand;
Button viewDetails;
Button addToCart;
}
}