我一直在尝试在我的Activity中实现搜索ListView,它使用自定义适配器。列表是从特定URL获取的。这就是我尝试过的。错误是"方法getName()未定义类型String"。请帮忙。
MainActivity.java
package com.example.saregama;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLEncoder;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity{
Activity context;
HttpPost httppost;
StringBuffer buffer;
HttpResponse response;
HttpClient httpclient;
ProgressDialog pd;
CustomAdapter adapter;
ListView listProduct;
ArrayList<String> records;
String mname;
Intent i;
Intent j;
BackTask bt;
Intent k;
EditText et;
protected void onCreate(Bundle savedInstanceState) {
//TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
// WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
context=this;
records=new ArrayList<String>();
listProduct=(ListView)findViewById(R.id.product_list);
et = (EditText)findViewById(R.id.search_movies);
i = new Intent(this, ReportActivity.class);
j = new Intent(this, RequestActivity.class);
k = new Intent(this, AboutDeveloper.class);
Intent iin= getIntent();
Bundle b = iin.getExtras();
if(b!=null)
{
mname =(String) b.getString("second_selection");
}
this.setTitle("All Movies - "+mname);
adapter=new CustomAdapter(context, R.layout.list_item,R.id.pro_name, records);
listProduct.setAdapter(adapter);
bt=new BackTask();
bt.execute();
listProduct.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long id){
String sText = ((TextView) v.findViewById(R.id.pro_name)).getText().toString();
Intent songIntent = new Intent(getApplicationContext(), SongActivity.class);
songIntent.putExtra("movie_name", sText );
startActivity(songIntent);
}
});
et.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
int textlength = cs.length();
ArrayList<String> records = new ArrayList<String>();
for(String c: records){
if (textlength <= c.getName().length()) {
if (c.getName().toLowerCase().contains(cs.toString().toLowerCase())) {
records.add(c);
}
}
}
listProduct.setAdapter(adapter);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
//create a LayoutTransition object
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch(item.getItemId()){
case R.id.report:
startActivity(i);
break;
case R.id.request:
startActivity(j);
break;
case R.id.action_refresh:
finish();
startActivity(getIntent());
break;
// case R.id.aboutme:
// startActivity(k);
// break;
}
return true;
}
//background process to make a request to server and list product information
private class BackTask extends AsyncTask<Void,Void,Void>{
protected void onPreExecute(){
super.onPreExecute();
pd = new ProgressDialog(context);
pd.setTitle("Retrieving data");
pd.setMessage("Please wait.");
pd.setCancelable(true);
pd.setIndeterminate(true);
pd.show();
}
protected Void doInBackground(Void...params){
InputStream is=null;
String result="";
try{
records.clear();
String query = URLEncoder.encode(mname, "utf-8");
httpclient=new DefaultHttpClient();
httppost= new HttpPost("http://necrecords.16mb.com/getproducts.php?password="+query);
response=httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
if(pd!=null)
pd.dismiss(); //close the dialog if error occurs
Log.e("ERROR",e.getMessage());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("ERROR", "Error converting result "+e.toString());
}
//parse json data
try{
JSONArray jArray =new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
String record=json_data.getString("pname")+"__"+json_data.getInt("uprice");
records.add(record);
}
}
catch(Exception e){
Log.e("ERROR", "Error pasting data "+e.toString());
}
return null;
}
protected void onPostExecute(Void result){
if(pd!=null) pd.dismiss(); //close dialog
adapter.notifyDataSetChanged(); //notify the ListView to get new records
}
}
}
CustomAdapter.java
package com.example.saregama;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class CustomAdapter extends ArrayAdapter<String> {
int groupid;
ArrayList<String> records;
Context context;
public CustomAdapter(Context context, int vg, int id, ArrayList<String> records){
super(context,vg, id, records);
this.context=context;
groupid=vg;
this.records=records;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(groupid, parent, false);
String[] row_items=records.get(position).split("__");
TextView textName= (TextView) itemView.findViewById(R.id.pro_name);
textName.setText(row_items[0]);
TextView textPrice= (TextView) itemView.findViewById(R.id.pro_uprice);
textPrice.setText(row_items[1]+"$");
return itemView;
}
}
答案 0 :(得分:1)
在onTextChanged
中,您正在创建List
个String
个getName
并试图在每个getName
上调用String
。正如您的错误消息所示,{{1}}不是{{1}}上的方法,因此无法编译。