从适配器

时间:2017-11-21 05:35:05

标签: java android getter-setter

我想获取列表视图中所选元素的TextView的字符串值,该值通过JSON数据,适配器和getter&二传手术方法。我尝试了多种方法,但无法获取文本的String值。

我将传递此字符串值,以JSON的形式进一步获取与此选定项目相关的数据并开始新活动。

以下是我从中选择项目的活动。评论的行是我尝试过的所有内容。

public class Cat_Home extends AppCompatActivity {

String json_string;
JSONObject jsonObject;
JSONArray jsonArray;
ListView listView;
CategoryHomeAdapter categoryHomeAdapter;
String MOV_SUM_JSON,mov_sum_json;
String final_movie_name;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cat__home);

    listView = (ListView)findViewById(R.id.list_cat_home);
    categoryHomeAdapter = new CategoryHomeAdapter(this,R.layout.list_cat_home_element);
    listView.setAdapter(categoryHomeAdapter);
    json_string = getIntent().getExtras().getString("json_data");

    try {
        jsonObject = new JSONObject(json_string);
        jsonArray = jsonObject.getJSONArray("server_response");
        int count=0;
        String name;

        while(count<jsonArray.length()){
            JSONObject JO = jsonArray.getJSONObject(count);
            name = JO.getString("name");

            CategoryH categoryH = new CategoryH(name);
            categoryHomeAdapter.add(categoryH);
            count++;
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }

    listView.setOnItemClickListener(onItemClickListener);

}

AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

        //ListView listView1 = findViewById(R.id.list_cat_home);

        /*CategoryHolder1 categoryHolder1;
        categoryHolder1 = new CategoryHolder1();
        CategoryH categoryH1 = (CategoryH) categoryHomeAdapter.getItem(i);
        categoryHolder1.tx_name1.setText((categoryH1.getName()));*/


        //CategoryH categoryH = new CategoryH();
        //TextView textView;
        //textView.setText(categoryH.getName());
        //String selected_movie_name = categoryH1.getName();
        String selected_movie_name = String.valueOf(adapterView.getItemAtPosition(i));
        //categoryHolder1.categoryhold1();
        //String selected_movie_name = final_movie_name;
        //Log.d("tag_final_movie_name",final_movie_name);
        //String selected_movie_name = (String) categoryHomeAdapter.getItem(i);

        //String selected_movie_name = String.valueOf(listView.getItemAtPosition(i));
        //String selected_movie_name = listView.getItemAtPosition(i).toString();
        Log.d("tag_selected_movie_name",selected_movie_name);
        BackgroundTaskGetMovSum backgroundTaskGetMovSum = new BackgroundTaskGetMovSum();
        backgroundTaskGetMovSum.execute(selected_movie_name);
    }
};

我获取所选电影名称的日志如下。由于字符串值不是来自selected_movie_name,所以当我使用它来获取JSON时,我得到一个空响应。

D/tag_selected_movie_name: com.bmps.android.bollywoodmoviesplotsummary.CategoryH@79cae38
D/tag_mov_json: {"server_response":[]}

以下是我的适配器文件

public class CategoryHomeAdapter extends ArrayAdapter {

List list = new ArrayList();

public CategoryHomeAdapter(@NonNull Context context, int resource) {
    super(context, resource);

}

@Override
public void add(Object object) {
    super.add(object);
    list.add(object);
}

@Override
public int getCount() {
    return list.size();
}

@Nullable
@Override
public Object getItem(int position) {
    return list.get(position);
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    View row;
    row =convertView;
    CategoryHolder categoryHolder;
    if (row==null){
        LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = layoutInflater.inflate(R.layout.list_cat_home_element,parent,false);
        categoryHolder = new CategoryHolder();
        categoryHolder.tx_name = (TextView)row.findViewById(R.id.movie_title);
        row.setTag(categoryHolder);
    }
    else{
        categoryHolder = (CategoryHolder) row.getTag();
    }

    CategoryH categoryH = (CategoryH) this.getItem(position);
    categoryHolder.tx_name.setText(categoryH.getName());

    return row;
}

static class CategoryHolder{
    TextView tx_name;
}
}

CategoryH类

public class CategoryH {

private String name;

public CategoryH(String name){
    this.setName(name);
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}
}

0 个答案:

没有答案